What's wrong with using == to compare floats in Java?

后端 未结 21 2234
你的背包
你的背包 2020-11-22 01:00

According to this java.sun page == is the equality comparison operator for floating point numbers in Java.

However, when I type this code:



        
21条回答
  •  孤城傲影
    2020-11-22 01:37

    Foating point values are not reliable, due to roundoff error.

    As such they should probably not be used for as key values, such as sectionID. Use integers instead, or long if int doesn't contain enough possible values.

提交回复
热议问题