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

后端 未结 21 1873
你的背包
你的背包 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:18

    Are you dealing with outsourced code that would use floats for things named sectionID and currentSectionID? Just curious.

    @Bill K: "The binary representation of a float is kind of annoying." How so? How would you do it better? There are certain numbers that cannot be represented in any base properly, because they never end. Pi is a good example. You can only approximate it. If you have a better solution, contact Intel.

提交回复
热议问题