开发者

Floating point error in representation?

开发者 https://www.devze.com 2023-04-08 19:01 出处:网络
when i make this multiplication 0.94 * 8700 the output is 8177.999999999999 but it should have been 8178

when i make this multiplication

0.94 * 8700

the output is

8177.999999999999

but it should have been

8178

开发者_JS百科

i'm using java , but i don't think this error is related to a particular Programming language now my question is ... why this happened ??

and what other numbers (just as an example) cause the same error?


The specific reason in your case is that the real number 0.94 cannot be represented exactly in a double precision floating point. When you type 0.94, the actual number stored is 0.939999999999999946709294817992486059665679931640625.


It isn't an error. IEEE floats can't represent decimal numbers exactly.

0

精彩评论

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

关注公众号