JUnit断言:在浮点数之间进行断言 [英] JUnit assertions : make the assertion between floats

查看:143
本文介绍了JUnit断言:在浮点数之间进行断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个值:一个是字符串,另一个是float 因此,我将字符串转换为float,然后尝试调用assertEquals(val1,val2),但这未经授权,我猜assertEquals不接受float作为参数.

I need to compare two values : one a string and the other is float so I convert the string to float then try to call assertEquals(val1,val2) but this is not authorized , I guess that the assertEquals doesn't accept float as arguments.

在这种情况下,我的解决方案是什么?

What is the solution for me in this case ?

推荐答案

您必须为Floats的断言提供一个增量:

You have to provide a delta to the assertion for Floats:

Assert.assertEquals(expected, actual, delta)

虽然delta是预期值与实际值之间的最大差值(delta),但两个值仍被视为相等.

While delta is the maximum difference (delta) between expected and actual for which both numbers are still considered equal.

Assert.assertEquals(0.0012f, 0.0014f, 0.0002); // true
Assert.assertEquals(0.0012f, 0.0014f, 0.0001); //false

这篇关于JUnit断言:在浮点数之间进行断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆