C ++ pow函数得到一个奇怪的结果 [英] C++ pow function get a weird result
本文介绍了C ++ pow函数得到一个奇怪的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用某些版本的minGW,以下代码将打印99。
Using some version of minGW, the following code will print 99.
int high;
high = pow(10,2);
std::cout<<high<<std::endl;
pow函数的参数是double,但是为什么我得到99?
The parameter of pow function is double, but why i get 99? Someone who can tell me the process hidden inside pow function ?
推荐答案
将double转换为整数会截断小数部分。 pow(10,2)
产生略微不准确的结果;
Converting a double to an integer truncates the fractional part. pow(10,2)
produces a slightly inaccurate result; if it's slightly high, you'll get 100 and if it is slightly low you'll get 99.
道德:如果你的意思是 i *,你会得到100, i
,写入 i * i
。
Moral: if you mean i*i
, write i*i
.
这篇关于C ++ pow函数得到一个奇怪的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文