简体   繁体   中英

Why is output of {42.05 + 0.05 } like this on Dart Lang?

When i try this on DartPad output is like this. Anyone can explain ?

在此处输入图片说明

This is expected behavior. Double numbers cannot represent all decimal fractions precisely, and neither 0.05 nor 42.05 are the exact values that the double values represent. The exact values are:

  • 42.0499999999999971578290569595992565155029296875
  • 0.05000000000000000277555756156289135105907917022705078125

If you add these two exact values, the result can yet again not be represented exactly as a double. The two closest representable doubles are:

  • 42.099999999999994315658113919198513031005859375
  • 42.10000000000000142108547152020037174224853515625

Of these, the former is closer to the correct result of the addition, so that is the double value chosen to represent that result.

This issue is not specific to Dart. All language using IEEE-754 64-bit floating point numbers will get the same result, and that is probably all languages with a 64-bit floating point type (C, C++, C#, Java, JavaScript, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM