2

Let's say I've an array of unsigned long long, is there a way to put the overflow in the next ull? I want exact number so double aren't an option. For now this is what I have:

#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
    }
}
AdminXVII
  • 1,319
  • 11
  • 22
  • There's no simple solution unless your compiler has an integer type wider than `unsigned long long` – M.M Apr 23 '16 at 12:19
  • 2
    If you only need it to work for the case `unsigned long long` = 64-bit, then (a) use `uint64_t` instead, and (b) [see this thread](http://stackoverflow.com/questions/25095741/how-can-i-multiply-64-bit-operands-and-get-128-bit-result-portably) – M.M Apr 23 '16 at 12:20

0 Answers0