## digitalmars.D.learn - float comparison

• nocide (12/12) Aug 25 2012 unittest {
• Jonathan M Davis (5/19) Aug 25 2012 Comparing floating point numbers with == is almost always the wrong thin...
• nocide (3/22) Aug 25 2012 Many thanks!
• bearophile (5/7) Aug 25 2012 The second part of the answer is a suggestion to use
nocide <benutzer example.com> writes:
```unittest {
bool fuzzyCmp(float a, float b) {
return abs(a-b) < 0.000001f;
}
float len1 = sqrt(8.0f);
float len2 = sqrt(8.0f);
assert(len1 == len2);              // passes
assert(fuzzyCmp(len1,sqrt(8.0f))); // passes
assert(len1 == sqrt(8.0f));        // fails!!
}

The comparison of the float variable passes, but the comparison to the
return value of the sqrt-function fails.?!
```
Aug 25 2012
Jonathan M Davis <jmdavisProg gmx.com> writes:
```On Saturday, August 25, 2012 10:28:44 nocide wrote:
unittest {
bool fuzzyCmp(float a, float b) {
return abs(a-b) < 0.000001f;
}
float len1 = sqrt(8.0f);
float len2 = sqrt(8.0f);
assert(len1 == len2);              // passes
assert(fuzzyCmp(len1,sqrt(8.0f))); // passes
assert(len1 == sqrt(8.0f));        // fails!!
}

The comparison of the float variable passes, but the comparison to the
return value of the sqrt-function fails.?!

Comparing floating point numbers with == is almost always the wrong thing to
do.

http://floating-point-gui.de/

- Jonathan M Davis
```
Aug 25 2012
nocide <benutzer example.com> writes:
```Am 25.08.2012 10:36, schrieb Jonathan M Davis:
On Saturday, August 25, 2012 10:28:44 nocide wrote:
unittest {
bool fuzzyCmp(float a, float b) {
return abs(a-b) < 0.000001f;
}
float len1 = sqrt(8.0f);
float len2 = sqrt(8.0f);
assert(len1 == len2);              // passes
assert(fuzzyCmp(len1,sqrt(8.0f))); // passes
assert(len1 == sqrt(8.0f));        // fails!!
}

The comparison of the float variable passes, but the comparison to the
return value of the sqrt-function fails.?!

Comparing floating point numbers with == is almost always the wrong thing to
do.

http://floating-point-gui.de/

- Jonathan M Davis

Many thanks!

nocide
```
Aug 25 2012
"bearophile" <bearophileHUGS lycos.com> writes:
```Jonathan M Davis:

Comparing floating point numbers with == is almost always the
wrong thing to do.

The second part of the answer is a suggestion to use
std.math.feqrel.

Bye,
bearophile
```
Aug 25 2012