workhorsy / BDD

Behavior Driven Development testing framework for the D programming language
Boost Software License 1.0
3 stars 1 forks source link

Add shouldBeClose for comparing floating point #17

Open workhorsy opened 3 years ago

workhorsy commented 3 years ago

Comparing equality of different floating points is a bad idea.

Fails on LDC from precision loss between string and double:

import std.conv : to;
("13.1").to!double.shouldEqual(13.1);

Comparing double to float fails:

(13.1f).shouldEqual(13.1);

Use std.math.isClose instead: https://dlang.org/phobos/std_math_operations.html#isClose

(13.1f).shouldBeClose(13.1);
(13.1f).shouldBeClose(13.1, 1e-9);

Also make shouldEqual fail to compile with floating point numbers, and recommend shouldBeClose instead.