Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

javascript - Strange result with floating point addition

Why is it when I print/display the result of

eval("11.05") + eval("-11")

it comes out as 0.05000000000000071 instead of the expected 0.05. Is there something I am missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This has nothing to do with eval. In fact, this is what happens if you type in a console 11.05 - 11: enter image description here

This is a consequence of how programming languages store floating-point numbers; they include a small error. If you want to read more about this, check this out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...