avatar
fireworks99
keep hungry keep foolish

关于__int128

__int128

编译器的gcc不支持这种数据类型,在codeblocks/Dev C++中无法编译

推荐在线编辑器 :http://ideone.com/

大小

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    cout << sizeof(int) << '\n';
    cout << sizeof(long long) << '\n';
    cout << sizeof(long double) << '\n';
    cout << sizeof(__int128) << '\n';
    return 0;
}

Output:

4 = 21 4748 3647

8 ≈ 9.2233720368548 * 10 ^ 18

16 ≈ 1.7014118346047 * 10 ^ 38

16 ≈ 1.7014118346047 * 10 ^ 38

输入输出需自己写函数

Code of Input

void scan(__int128 &x)//输入
{
    x = 0;
    int f = 1;
    char ch;
    if((ch = getchar()) == '-') f = -f;
    else x = x*10 + ch-'0';
    while((ch = getchar()) >= '0' && ch <= '9')
        x = x*10 + ch-'0';
    x *= f;
}

Code of Output

void print(__int128 x)
{
    if(x < 0)
    {
        x = -x;
        putchar('-');
    }
     if(x > 9) print(x/10);
    putchar(x%10 + '0');
}

还有一件事!!!

看以下代码

#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    int a, b;
    a = b = 2100000000;
    long long c = a + b;
    cout << c << '\n';
    return 0;
}

Output: -94967296

a、b皆为int类型,相加默认还是int,所以这里int溢出了,得到一个负值,再赋给一个long long类型的值已经晚了

    int a, b;
    a = b = 2100000000;
    long long c = a;
    c += b;

或者一开始a、b就设为long long类型

Site by Baole Zhao | Powered by Hexo | theme PreciousJoy