Precision of double after decimal point

人盡茶涼 提交于 2019-11-28 08:02:34

问题


In the lunch break we started debating about the precision of the double value type.

My colleague thinks, it always has 15 places after the decimal point.

In my opinion one can't tell, because IEEE 754 does not make assumptions about this and it depends on where the first 1 is in the binary representation. (i.e. the size of the number before the decimal point counts, too)

How can one make a more qualified statement?


回答1:


As stated by the C# reference, the precision is from 15 to 16 digits (depending on the decimal values represented) before or after the decimal point.

In short, you are right, it depends on the values before and after the decimal point.

For example:

  • 12345678.1234567D //Next digit to the right will get rounded up
  • 1234567.12345678D //Next digit to the right will get rounded up

Full sample at: http://ideone.com/eXvz3

Also, trying to think about double value as fixed decimal values is not a good idea.




回答2:


You're both wrong. A normal double has 53 bits of precision. That's roughly equivalent to 16 decimal digits, but thinking of double values as though they were decimals leads to no end of confusion, and is best avoided.

That said, you are much closer to correct than your colleague--the precision is relative to the value being represented; sufficiently large doubles have no fractional digits of precision.

For example, the next double larger than 4503599627370496.0 is 4503599627370497.0.




回答3:


C# doubles are represented according to IEEE 754 with a 53 bit significand p (or mantissa) and a 11 bit exponent e, which has a range between -1022 and 1023. Their value is therefore

p * 2^e

The significand always has one digit before the decimal point, so the precision of its fractional part is fixed. On the other hand the number of digits after the decimal point in a double depends also on its exponent; numbers whose exponent exceeds the number of digits in the fractional part of the significand do not have a fractional part themselves.

What Every Computer Scientist Should Know About Floating-Point Arithmetic is probably the most widely recognized publication on this subject.




回答4:


Since this is the only question on SO that I could find on this topic, I would like to make an addition to jorgebg's answer.

According to this, precision is actually 15-17 digits. An example of a double with 17 digits of precision would be 0.92107099070578813 (don't ask me how I got that number :P)



来源:https://stackoverflow.com/questions/12089817/precision-of-double-after-decimal-point

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