Prev: floating point issue?
Next: FAQ 8.14 How do I modify the shadow password file on a Unix system?
From: kevin0051 on 2 Mar 2010 20:55 I made a perl program as follows. ----------------- $AAA = 4.31; $AAA *= 100; printf ("%f\n", $AAA); printf ("%d\n", $AAA); ---------------- The output of this program is 431.000000 430 I don't know why the second output is 430 instead of 431. Can anyone help? Thanks Kevin
From: sreservoir on 2 Mar 2010 20:55 On 3/2/2010 8:54 PM, kevin0051 wrote: > Can anyone help? yes -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: sreservoir on 2 Mar 2010 20:56 On 3/2/2010 8:54 PM, kevin0051 wrote: > The output of this program is > 431.000000 > 430 > > I don't know why the second output is 431 instead of 431. > Can anyone help? yes -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe"
From: John Bokma on 2 Mar 2010 21:11 kevin0051 <kevin0051(a)gmail.com> writes: > I made a perl program as follows. > > ----------------- > $AAA = 4.31; > $AAA *= 100; > printf ("%f\n", $AAA); > printf ("%d\n", $AAA); > ---------------- > > The output of this program is > 431.000000 > 430 > > I don't know why the second output is 430 instead of 431. > Can anyone help? http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems -- John Bokma j3b Hacking & Hiking in Mexico - http://johnbokma.com/ http://castleamber.com/ - Perl & Python Development
From: Tad McClellan on 2 Mar 2010 22:25 kevin0051 <kevin0051(a)gmail.com> wrote: > I made a perl program as follows. You would have the same problem with any programming language. The problem is related to how numbers are implemented on computers rather than how any particular language behaves. > ----------------- > $AAA = 4.31; > $AAA *= 100; > printf ("%f\n", $AAA); > printf ("%d\n", $AAA); > ---------------- > > The output of this program is > 431.000000 > 430 > > I don't know why the second output is 430 instead of 431. See if running this gives you any clues: ----------------- #!/usr/bin/perl use warnings; use strict; my $AAA = 4.31; $AAA *= 100; printf ("%30.20f\n", $AAA); printf ("%d\n", $AAA); printf ("%.0f\n", $AAA); ----------------- see also: perldoc -q numbers Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
|
Next
|
Last
Pages: 1 2 3 Prev: floating point issue? Next: FAQ 8.14 How do I modify the shadow password file on a Unix system? |