JUnit assertEquals(双倍期望,双倍实际,双倍epsilon) [英] JUnit assertEquals(double expected, double actual, double epsilon)

查看:96
本文介绍了JUnit assertEquals(双倍期望,双倍实际,双倍epsilon)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
JUnit:对于double值的assertEquals

Possible Duplicate:
JUnit: assertEquals for double values

显然,assertEquals(double expected, double actual)已被弃用.

考虑到JUnit的广泛使用,令人惊讶地缺少JUnit的javadocs.您能告诉我如何使用新的assertEquals(double expected, double actual, double epsilon)吗?

The javadocs for JUnit are surprisingly lacking, considerings its wide use. Can you show me how to use the new assertEquals(double expected, double actual, double epsilon)?

推荐答案

Epsilon是您的模糊因素",因为双精度可能并不完全相等. Epsilon让您描述它们之间必须有多近.

Epsilon is your "fuzz factor," since doubles may not be exactly equal. Epsilon lets you describe how close they have to be.

如果您期望的是3.14159,但是需要3.14059到3.14259之间的任何值(即0.001以内),那么您应该编写类似

If you were expecting 3.14159 but would take anywhere from 3.14059 to 3.14259 (that is, within 0.001), then you should write something like

double myPi = 22.0d / 7.0d; //Don't use this in real life!
assertEquals(3.14159, myPi, 0.001);

(顺便说一句,22/7达到3.1428+,并且会失败声明.这是一件好事.)

(By the way, 22/7 comes out to 3.1428+, and would fail the assertion. This is a good thing.)

这篇关于JUnit assertEquals(双倍期望,双倍实际,双倍epsilon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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