弄清楚了
如果你手动转换到 unsigned long long,那就是 unsigned long long + unsigned long long
是整数运算,这不会有问题
但是如果你没有手动转换到 unsigned long long,那编译器就自动转换到 double,是把 unsigned long long 自动转换到 double
进行浮点运算,浮点运算是有精度损失的
看下面的程序
如果是整数运算,在最后一次加法之前
temp: 9223372036854775808
sum: 9223372036854775807
temp + sum 的结果是
s: 18446744073709551615
但是,如果是浮点运算
temp: 9223372036854775808.000000
a: 9223372036854775808
s: 0
9223372036854775808 + 9223372036854775808 = 18446744073709551616
18446744073709551616 已经超过了 unsigned long long 能表示的范围,加法溢出了
对于当前这个程序来说,浮点运算的精度是从 i == 53 开始 出现问题的
#include <stdio.h>
#include <math.h>
int main()
{
unsigned long long sum = 0;
unsigned long long temp;
unsigned long long weight;
int i;
for (i=0; i < 64; i++)
{
temp = pow(2, i);
if(i == 63) {
printf("temp: %llu\n", temp);
printf("sum: %llu\n", sum);
unsigned long long s = sum + temp;
printf("s: %llu\n", s);
}
sum = sum + temp;
}
weight = sum / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", sum);
printf("如果每25000粒麦子为1kg,那么应该给%llu公斤麦子!\n", weight);
return 0;
}
/*
temp: 9223372036854775808
sum: 9223372036854775807
s: 18446744073709551615
舍罕王应该给予达依尔18446744073709551615粒麦子!
如果每25000粒麦子为1kg,那么应该给737869762948382公斤麦子!
*/
#include <stdio.h>
#include <math.h>
int main() {
unsigned long long a = 0;
int i;
for(i = 0; i < 64; i++) {
double temp = pow(2, i);
if(i == 63) {
printf("temp: %lf\n", temp);
printf("a: %llu\n", a);
unsigned long long s = a + temp;
printf("s: %llu\n", s);
}
a += temp;
}
unsigned long long b = a / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", a);
printf("如果每25000粒麦子为1kg,那么应该给%llukg麦子!\n", b);
return 0;
}
/*
temp: 9223372036854775808.000000
a: 9223372036854775808
s: 0
舍罕王应该给予达依尔0粒麦子!
如果每25000粒麦子为1kg,那么应该给0kg麦子!
*/
#include <stdio.h>
#include <math.h>
int main()
{
unsigned long long sum = 0;
unsigned long long temp;
unsigned long long weight;
int i;
for (i=0; i < 64; i++)
{
temp = pow(2, i);
if(i == 52) {
printf("temp: %llu\n", temp);
printf("sum: %llu\n", sum);
unsigned long long s = sum + temp;
printf("s: %llu\n", s);
}
sum = sum + temp;
}
weight = sum / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", sum);
printf("如果每25000粒麦子为1kg,那么应该给%llu公斤麦子!\n", weight);
return 0;
}
/*
temp: 4503599627370496
sum: 4503599627370495
s: 9007199254740991
舍罕王应该给予达依尔18446744073709551615粒麦子!
如果每25000粒麦子为1kg,那么应该给737869762948382公斤麦子!
*/
#include <stdio.h>
#include <math.h>
int main() {
unsigned long long a = 0;
int i;
for(i = 0; i < 64; i++) {
double temp = pow(2, i);
if(i == 52) {
printf("temp: %lf\n", temp);
printf("a: %llu\n", a);
unsigned long long s = a + temp;
printf("s: %llu\n", s);
}
a += temp;
}
unsigned long long b = a / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", a);
printf("如果每25000粒麦子为1kg,那么应该给%llukg麦子!\n", b);
return 0;
}
/*
temp: 4503599627370496.000000
a: 4503599627370495
s: 9007199254740991
舍罕王应该给予达依尔0粒麦子!
如果每25000粒麦子为1kg,那么应该给0kg麦子!
*/
#include <stdio.h>
#include <math.h>
int main()
{
unsigned long long sum = 0;
unsigned long long temp;
unsigned long long weight;
int i;
for (i=0; i < 64; i++)
{
temp = pow(2, i);
if(i == 53) {
printf("temp: %llu\n", temp);
printf("sum: %llu\n", sum);
unsigned long long s = sum + temp;
printf("s: %llu\n", s);
}
sum = sum + temp;
}
weight = sum / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", sum);
printf("如果每25000粒麦子为1kg,那么应该给%llu公斤麦子!\n", weight);
return 0;
}
/*
temp: 9007199254740992
sum: 9007199254740991
s: 18014398509481983
舍罕王应该给予达依尔18446744073709551615粒麦子!
如果每25000粒麦子为1kg,那么应该给737869762948382公斤麦子!
*/
#include <stdio.h>
#include <math.h>
int main() {
unsigned long long a = 0;
int i;
for(i = 0; i < 64; i++) {
double temp = pow(2, i);
if(i == 53) {
printf("temp: %lf\n", temp);
printf("a: %llu\n", a);
unsigned long long s = a + temp;
printf("s: %llu\n", s);
}
a += temp;
}
unsigned long long b = a / 25000;
printf("舍罕王应该给予达依尔%llu粒麦子!\n", a);
printf("如果每25000粒麦子为1kg,那么应该给%llukg麦子!\n", b);
return 0;
}
/*
temp: 9007199254740992.000000
a: 9007199254740991
s: 18014398509481984
舍罕王应该给予达依尔0粒麦子!
如果每25000粒麦子为1kg,那么应该给0kg麦子!
*/
|