java – Float vs Double

Is there a case where compare (equals()) between two floating-point values returns false if they are compared to double and true if they are compared to float

As part of my team project, I am writing programs to compare two values of any given type There are four types that I have to deal with completely: double, float, int and long So I want to combine double and float into a function, that is, I just double and compare any floating point number

Will this lead to any incorrect results?

thank you.

Solution

If you convert double precision to floating point number and the difference between them exceeds the precision of floating point type, you may encounter trouble

For example, suppose you have two double values:

9.876543210
9.876543211

And the precision of floating point number is only six decimal digits This means that the two floating-point values will be 9.87654, so they are equal, even if the double values themselves are not equal

However, if you are talking about projecting buoys to doubles, the same buoy should give you the same doubles If the buoy is different, additional accuracy will ensure that doubles are also different

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>