为什么pow(10,5)= 9,999在C
发布时间:2020-12-16 05:30:05 所属栏目:百科 来源:网络整理
导读:最近我写了一个代码块: const int sections = 10;for(int t= 0; t 5; t++){ int i = pow(sections,5- t -1); cout i endl;} 结果是错误的: 9999100099101 如果我使用这个代码: for(int t = 0; t 5; t++){ cout pow(sections,5-t-1) endl; } 问题不再发生
最近我写了一个代码块:
const int sections = 10; for(int t= 0; t < 5; t++){ int i = pow(sections,5- t -1); cout << i << endl; } 结果是错误的: 9999 1000 99 10 1 如果我使用这个代码: for(int t = 0; t < 5; t++){ cout << pow(sections,5-t-1) << endl; } 问题不再发生了: 10000 1000 100 10 1 有人给我一个解释吗?非常感谢你! 解决方法
由于浮点值的表示,pow(10.0,5)可以是9999.9999999或类似的东西.当您将其分配给被截断的整数.
编辑:在cout < pow(10.0,5);它看起来像输出是四舍五入的,但我现在没有任何支持文件确认. 编辑2:BoBTFish和this question发表的评论证实当pow(10.0,5)被直接用于正在变圆的cout中时. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |