开发者

What Should be the result of 128.01+0.01 in C# [duplicate]

开发者 https://www.devze.com 2023-02-10 21:37 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicates: Precision of Floating Point
This question already has answers here: Closed 11 years ago.

Possible Duplicates:

Precision of Floating Point

Floating point arithmetic is too reliable.

Hi Guys,

I came across a rather strange looking problem, i am running a loop from 82.01 to 169.06 in steps of 0.01 but when i reach 128.0开发者_如何转开发1 and do (128.01+0.01) it gives 128.019999999998 instead of 128.02. I am using double for all these computations. If i use decimal to do these computations it works out fine, am i missing a very basic funda here, i found some articles and discussions on the web explaining that decimal is the correct data type to do these computations but still a basic computation like (128.01+0.01) should give correct results.


Floating-point calculations are inherently inaccurate, so, this behaviour is completely normal. If you really need higher accuracy, stick to decimals, but keep in mind that they are way, way slower than floats/doubles. Usually, it's better to just accept the inaccuracies and round as needed.


Here is the scientific detailed explanation of your issue

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号