On Doubles and Equality

前端 未结 3 655
粉色の甜心
粉色の甜心 2020-12-19 23:29

One of the methods i have returns a double. When testing this method as part of my jUnit, i noticed the following oddity:

    Strin         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 00:01

    Comparing doubles in general is dangerous, because floating-point representations are, by definition, inexact. Furthermore, you have to be careful when printing values out, as the printed representation is often rounded compared to the actual stored representation.

    That said, @JBNizet nailed it -- you're writing a Java assertion, not a JUnit test!

提交回复
热议问题