[TAG] Talkback:143/sephton.html
Mauro Orlandini - IASF/Bologna
orlandini at iasfbo.inaf.it
Tue Oct 2 11:53:23 MSD 2007
Talking about rounding, I almost went crazy to find out why a perl program
I was writing did not give the right result. Here is the code:
----------------------------------------------
#!/usr/bin/perl
#
$f = 4.95;
$F = (4.8+5.1)/2;
$F_spr = sprintf("%.10f", (4.8+5.1)/2);
printf " f: %32.30f\n", $f;
printf " F: %32.30f\n", $F;
printf " F_spr: %32.30f\n", $F_spr;
if ($f == $F) { print "\n f equals F!\n\n"; }
--------------------------------------------------
The test fails even if $f and $F are the same! I solved by using $F_spr
instead of $F, but it took me a day to find it out... 8-(
Ciao, Mauro
--
__^__ __^__
( ___ )------------------------------------------------------------------( ___ )
| / | Mauro Orlandini Email: orlandini at iasfbo.inaf.it | \ |
| / | INAF/IASF Bologna Voice: +39-051-639-8667 | \ |
| / | Via Gobetti 101 Fax: +39-051-639-8723 | \ |
| / | 40129 Bologna - Italy WWW: http://www.iasfbo.inaf.it/~mauro/ | \ |
| / |--------------------------------------------------------------------| \ |
| / | Today's quote: | \ |
| / | Military Justice is to Justice what Military Music is to Music | \ |
|___| (Groucho Marx) |___|
(_____)------------------------------------------------------------------(_____)
^ ^
More information about the TAG
mailing list