Float vs Double

后端 未结 7 1831
-上瘾入骨i
-上瘾入骨i 2021-01-17 23:56

Is there ever a case where a comparison (equals()) between two floating point values would return false if you compare them as DOUBLE

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 00:10

    If you're converting doubles to floats and the difference between them is beyond the precision of the float type, you can run into trouble.

    For example, say you have the two double values:

    9.876543210
    9.876543211
    

    and that the precision of a float was only six decimal digits. That would mean that both float values would be 9.87654, hence equal, even though the double values themselves are not equal.

    However, if you're talking about floats being cast to doubles, then identical floats should give you identical doubles. If the floats are different, the extra precision will ensure the doubles are distinct as well.

提交回复
热议问题