Is it ok to use assertTrue on float and double values in JUnit?

こ雲淡風輕ζ 提交于 2019-12-01 23:03:34
Matthew Farwell

In general you can compare primitive types with ==, but with floating point values, two values can look the same (when formatted as a string) but be two different values. You should be using assertEquals(expected, actual, delta). See the answer to JUnit: assertEquals for double values.

Because double (and float) calculations are not necessarily exact, you can compare using a delta (effectively a margin of error).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!