Truncation after 17. Digit on C

人走茶凉 提交于 2019-12-02 13:04:12

double of accuracy is approximately 17 decimal digits(52bit+1). so You'll use the type that has a 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; i <= 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 than 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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!