From: J�rgen Exner on
kevin0051 <kevin0051(a)gmail.com> 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
discussions in this NG about this ever popular topic.

jue
From: Xho Jingleheimerschmidt on
John Bokma wrote:
> 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
>

This surprised me. I knew int() would truncate of course, but I thought
printf with would round in the same way for %d as it does for %.0f,
rather than truncate.

Xho
From: Steve C on
Xho Jingleheimerschmidt wrote:
> John Bokma wrote:
>> 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
>>
>
> This surprised me. I knew int() would truncate of course, but I thought
> printf with would round in the same way for %d as it does for %.0f,
> rather than truncate.
>

It shouldn't surprise you:

perl -e '$f = 4.31; $f *= 100; $h[$f] = 0; print $#h'
430

I would expect any use of a scalar as an integer to truncate.
Similarly:

perl -e '$f = "430plusalittle"; printf "%d",$f'
430

Using a string as an int stops at the first non-digit.
It should not take any of the rest of the string into account.
In the same way, %d should ignore any part of a float other than
the integer part.
From: Peter J. Holzer on
On 2010-03-03 03:25, Tad McClellan <tadmc(a)seesig.invalid> wrote:
> kevin0051 <kevin0051(a)gmail.com> wrote:
>> I made a perl program as follows.
>
> You would have the same problem with any programming language.

Except COBOL or PL/SQL or bc or ...


> The problem is related to how numbers are implemented on computers
> rather than how any particular language behaves.

Not "number implemented on computers" in general, but specific
implementations of numbers. Binary floating point numbers are very
common (because almost all modern processors support them in hardware)
but there are many other possible number implementations.

Decimal fixed or floating point numbers avoid the problem for decimal
fractions, which is sufficient for applications which need to represent
decimal fractions exactly (e.g. financial applications), but they still
have the problem for other fractions (1/3, 1/7, ...).

Rational numbers can be stored as a pair of (potentially big) integers.

There is no way to represent all numbers in a finite amount of memory of
course.

The number representation is not completely independent of the language.

C for example mandates that float, double, etc. are floating point
numbers. They can be binary, decimal or use some other base, but they
can't be fractions, for example.

COBOL provides decimal fixed point data types.

PL/SQL has a decimal floating point type (with 38 digits).

bc has arbitrary precision fixed point numbers.

Perl provides several number representations as modules.

hp

From: sreservoir on
On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
> On 2010-03-03 03:25, Tad McClellan<tadmc(a)seesig.invalid> wrote:
>> kevin0051<kevin0051(a)gmail.com> wrote:
>>> I made a perl program as follows.
>>
>> You would have the same problem with any programming language.
>
> Except COBOL or PL/SQL or bc or ...

bc is made for math and few people actually use the other two anymore.

of course, some lisps use fractions.

> [snipped]

> There is no way to represent all numbers in a finite amount of memory of
> course.

there is no way to represent all numbers full stop.

--

"Six by nine. Forty two."
"That's it. That's all there is."
"I always thought something was fundamentally wrong with the universe"