Unexpected results with doubles
Joseph Malle
malle at umich.edu
Mon Jan 7 19:57:14 UTC 2019
I am learning D. I was working on Project Euler #199 (possible
spoilers) and got some unexpected results. It's probably a
stupid mistake but I can't see it.
Here is an edited function from my program:
auto radius(const double r1, const double r2, const double r3) {
auto const k1 = 1/r1;
auto const k2 = 1/r2;
auto const k3 = 1/r3;
writeln();
writeln("1 ", [k1, k2, k3]);
writeln("2 ", [k1 * k2, k2 * k3, k3 * k1]);
writeln("3 ", [k1 * k2 + k2 * k3 + k3 * k1]);
assert(!isNaN(k1 * k2 + k2 * k3 + k3 * k1));
writeln("4 ", [sqrt(k1 * k2 + k2 * k3 + k3 * k1)]);
assert(!isNaN(sqrt(k1 * k2 + k2 * k3 + k3 * k1)));
auto rv = 1 / (k1 + k2 + k3 + 2.0 * sqrt(k1 * k2 + k2 * k3 + k3
* k1));
assert(!isNaN(rv));
writeln("radius ", [r1, r2, r3], " => ", rv);
writeln();
return rv;
}
Here is some output:
1 [41.7846, 6.4641, 6.4641]
2 [270.1, 41.7846, 270.1]
3 [581.985]
4 [24.1244]
radius [0.0239323, 0.154701, 0.154701] => 0.00971237
1 [41.7846, 6.4641, 6.4641]
2 [270.1, 41.7846, 270.1]
3 [581.985]
4 [nan]
radius [0.0239323, 0.154701, 0.154701] => 0.00971237
1 [41.7846, 6.4641, 6.4641]
2 [270.1, 41.7846, 270.1]
3 [581.985]
4 [nan]
radius [0.0239323, 0.154701, 0.154701] => 0.00971237
The "4 [nan]" is unexpected. Each time they have the same
input/same output. But sometimes the 4th line is nan and
sometimes it's not. The asserts never fail. I've seen this
unexpected nan a few times with other inputs for this function.
If I change it to:
auto x = sqrt(k1 * k2 + k2 * k3 + k3 * k1);
writeln("4 ", [x]);
assert(!isNaN(x));
Then the assert fails. I checked if the assert fails before the
writeln too (as a sanity check) and yes, x is always NaN it seems.
I am doing $dmd -run on the command line. Working on a
reasonably up to date Mac.
$ dmd --version
DMD64 D Compiler v2.083.1
Copyright (C) 1999-2018 by The D Language Foundation, All Rights
Reserved written by Walter Bright
More information about the Digitalmars-d-learn
mailing list