gpt4 book ai didi

c - 打印双变量内容

转载 作者:行者123 更新时间:2023-12-04 12:13:09 25 4
gpt4 key购买 nike

我尝试了以下代码片段,结果令我惊讶:

#include <stdio.h>
#include <math.h>

int main()
{
double num;
unsigned char ch;

ch = 19;
num = 1.0E+20 ;
num += ch * 1.0E+18;
printf("E18 = %lf \n",num);
printf("E18 = %e \n",num);

num = 11.0E+21 ;
num += ch * 1.0E+19;
printf("E19 = %lf <------\n",num);
printf("E19 = %e <------\n",num);

num = 11.0E+22 ;
num += ch * 1.0E+20;
printf("E20 = %lf\n",num);
printf("E20 = %e\n",num);

num = 11.0E+23 ;
num += ch * 1.0E+21;
printf("E21 = %lf\n",num);
printf("E21 = %e\n",num);

num = 11.0E+24 ;
num += ch * 1.0E+22;
printf("E22 = %lf <------\n",num);
printf("E22 = %e <------\n",num);
return 0;
}

程序的输出:

E18 = 119000000000000000000.000000 
E18 = 1.190000e+20
E19 = 11190000000000000524288.000000 <------
E19 = 1.119000e+22 <------
E20 = 111900000000000001048576.000000
E20 = 1.119000e+23
E21 = 1119000000000000044040192.000000
E21 = 1.119000e+24
E22 = 11189999999999999366660096.000000 <------
E22 = 1.119000e+25 <------

为什么在以指数形式打印时数据会损坏

最佳答案

因为,当数字变得足够大时,您会失去精度:http://en.wikipedia.org/wiki/Floating_point

关于c - 打印双变量内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2506691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com