From: Jarkko Hietaniemi Date: Tue, 26 Feb 2002 11:17:42 +0000 (+0000) Subject: Cray FP strikes again: things like 1.1 + 0.1 are easily X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=494244503fd690687b35aca99f4243ed5667eb4f;p=p5sagit%2Fp5-mst-13.2.git Cray FP strikes again: things like 1.1 + 0.1 are easily 1.1999999999999996, or thereabouts (yes, even sprintf). p4raw-id: //depot/perl@14884 --- diff --git a/t/base/num.t b/t/base/num.t index b3cde2e..f75e73d 100644 --- a/t/base/num.t +++ b/t/base/num.t @@ -68,20 +68,25 @@ print $a + 1 == 2 ? "ok 18\n" : "not ok 18 #" . $a + 1 . "\n"; $a = -1.; "$a"; print $a + 1 == 0 ? "ok 19\n" : "not ok 19 #" . $a + 1 . "\n"; +sub ok { # Can't assume too much of floating point numbers. + my ($a, $b, $c); + abs($a - $b) <= $c; +} + $a = 0.1; "$a"; -print $a + 1 == 1.1 ? "ok 20\n" : "not ok 20 #" . $a + 1 . "\n"; +print ok($a + 1, 1.1, 0.05) ? "ok 20\n" : "not ok 20 #" . $a + 1 . "\n"; $a = -0.1; "$a"; -print $a + 1 == 0.9 ? "ok 21\n" : "not ok 21 #" . $a + 1 . "\n"; +print ok($a + 1, 0.9, 0.05) ? "ok 21\n" : "not ok 21 #" . $a + 1 . "\n"; $a = .1; "$a"; -print $a + 1 == 1.1 ? "ok 22\n" : "not ok 22 #" . $a + 1 . "\n"; +print ok($a + 1, 1.1, 0.005) ? "ok 22\n" : "not ok 22 #" . $a + 1 . "\n"; $a = -.1; "$a"; -print $a + 1 == 0.9 ? "ok 23\n" : "not ok 23 #" . $a + 1 . "\n"; +print ok($a + 1, 0.9, 0.05) ? "ok 23\n" : "not ok 23 #" . $a + 1 . "\n"; $a = 10.01; "$a"; -print $a + 1 == 11.01 ? "ok 24\n" : "not ok 24 #" . $a + 1 . "\n"; +print ok($a + 1, 11.01, 0.005) ? "ok 24\n" : "not ok 24 #" . $a + 1 . "\n"; $a = 1e3; "$a"; print $a + 1 == 1001 ? "ok 25\n" : "not ok 25 #" . $a + 1 . "\n";