double - Truncation after 17. Digit on C -


i trying write simple program on c classical history between king, chessboard , rice grains. total amount of rice grains needed fill chessboard : 18 446 744 073 709 551 615 .

but i'm getting : 18 446 744 073 709 552 000. on c.

aren't there solution increase 17 digits resolution?

here code.

#include <stdio.h> #include <math.h>  int main( void ) {   double i=1.0;   while(i<=64)     {     printf("%2.0lf.casilla = %.0lf\n", i, pow(2.0,(i-1.0)));     i++;   }   printf("\n\n***en total = %.0lf granos.\n\n",pow(2.0,64.0)-1);   return 0; } 

thanks in advance!

double of accuracy approximately 17 decimal digits(52bit+1). you'll use type has 64-bit or higher accuracy.

#include <stdio.h> #include <stdint.h> #include <inttypes.h>  uint64_t pow_2_i(int n){//2^n     if(0 > n || n >= 64)         return 0;     uint64_t x = 1;     return x << n; }  int main(void){     int i;     uint64_t x, sum = 0;     for(i = 1; <= 64; ++i){         sum += (x = pow_2_i(i-1));         printf("%2d.casilla = %" priu64 "\n", i, x);     }     printf("\n\n***en total = %" priu64 " granos.\n\n", sum);//no cheat^-^     return 0; } 

if long double has large accuracy double

#include <stdio.h>  int main( void ){     long double x=1.0;//1.0l     int i;     for(i=1; i<=64;++i){         printf("%2d.casilla = %.0lf\n", i, x);         x *= 2.0l;     }     printf("\n\n***en total = %.0lf granos.\n\n", x-1.0l);//2^64-1:Σar^k (k=0->n) =a(r^(n+1)-1)/(r-1):1(2^(63+1)-1)/(2-1)     return 0; } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -