C中的另一个无符号长long溢出数组

时间:2016-04-23 12:16:01

标签: c c99 integer-overflow unsigned-long-long-int

让我们说一下unsigned long long的数组,有没有办法将溢出放在下一个ull?我想要确切的数字,所以双倍不是一个选择。 现在这就是我所拥有的:

#include <stdio.h>

unsigned long long d[2] = {1,0}, n[2] = {0,0}; // denominator, numerator
unsigned long long nd = 1;

int main(int argc,char* argv[])
{
    while(1){

        if(nd+2 < nd) // next denominator greater than 2^64-1
            return 0;
        // n = n*nd + d
        n[0] = n[0] * nd + d[0];
        // n[1] += *overflow    <------
        n[1] = n[1] * nd + d[1];
        d[0] *= nd;
        // d[1] += *overflow    <------
        d[1] *= nd;
        printf("current denominator: %llu -> 4*%llu/%llu\n", nd, n, d);
        nd += 2; //increment next denominator
    }
}

0 个答案:

没有答案