floating point issue?

K

kevin0051

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
 
S

Steve C

Xho said:
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.
 
P

Peter J. Holzer

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
 
J

Jürgen Exner

sreservoir said:
there is no way to represent all numbers full stop.

This is becoming philosophical now, but you can represent any number. In
the worst case you simply use a verbal description. Granted, that
doesn't do you much good if you want to use that number in a calculation
on the computer, but it is possible to represent it because otherwise
you wouldn't be able to think of this number in the first place.

jue
 
D

Dr.Ruud

Jürgen Exner said:
This is becoming philosophical now, but you can represent any number. In
the worst case you simply use a verbal description. Granted, that
doesn't do you much good if you want to use that number in a calculation
on the computer, but it is possible to represent it because otherwise
you wouldn't be able to think of this number in the first place.

I think you mean that number almost half way between 2 and 3.
;-)
 
J

Jürgen Exner

Dr.Ruud said:
I think you mean that number almost half way between 2 and 3.
;-)

No, the other one, the one that is exactly a quarter of an epsilon
smaller.

jue
 
K

Keith Thompson

Jürgen Exner said:
This is becoming philosophical now, but you can represent any number. In
the worst case you simply use a verbal description. Granted, that
doesn't do you much good if you want to use that number in a calculation
on the computer, but it is possible to represent it because otherwise
you wouldn't be able to think of this number in the first place.

As long as we're being philosophical, being able to think of a number
is not a prerequisite for that number's existence.

The set of finite verbal descriptions is only countably infinite.
There are uncountably infinitely many real numbers.

There are also some lovely paradoxes, such as
"the smallest number that cannot be described in 61 characters".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top