floating point issue?

Discussion in 'Perl Misc' started by kevin0051, Mar 3, 2010.

1. kevin0051Guest

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

kevin0051, Mar 3, 2010

2. sreservoirGuest

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, Mar 3, 2010

3. sreservoirGuest

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"

sreservoir, Mar 3, 2010
4. Jürgen ExnerGuest

kevin0051 <> 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