Velocity Reviews > Perl > floating point issue?

# floating point issue?

kevin0051
Guest
Posts: n/a

 03-03-2010
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 431 instead of 431.
Can anyone help?

Thanks
Kevin

sreservoir
Guest
Posts: n/a

 03-03-2010
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"

sreservoir
Guest
Posts: n/a

 03-03-2010
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"

Jürgen Exner
Guest
Posts: n/a

 03-03-2010
kevin0051 <(E-Mail Removed)> wrote:
>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 431 instead of 431.

I suppose you meant "430 instead of 431".

>Can anyone help?

Yes, among others your teacher for "Introduction into Fundamentals of
Basic Computer Numerics".

For more details please also try
printf("%.20f\n",\$AAA);
to force 20 digits after the decimal.

For further explanations please see "perldoc -q 999' or previous

jue