Why a+=b*pow(10,c-i-1) == 99 c++? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-01 01:23:52

The math.h and cmath versions of pow are slightly different. Basically there are more supported inputs for the cmath version. You can compare the two at these links.

math.h and cmath

As people have commented, it's probably related to variable type conversion and floating point errors. Pow operates on doubles, which can have floating point errors. Chances are pow is returning a value like 99.9999999 which is then being converted to an integer. Since the integer conversion truncates instead of rounds, you get 99.

corn3lius

from this link cplusplus.com

Additional overloads are provided in this header ( cmath ) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

I bet if you cast it it would be correct in math.h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!