Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Subject
- Posted on
- floating point issue?
- 03-03-2010
Re: floating point issue?
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
Re: floating point issue?
Xho Jingleheimerschmidt wrote:
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.
Re: floating point issue?
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.
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.liamg0cm.j.dat/"
Re: floating point issue?
Except COBOL or PL/SQL or bc or ...
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
Re: floating point issue?
On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
bc is made for math and few people actually use the other two anymore.
of course, some lisps use fractions.
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"
Re: floating point issue?
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
Re: floating point issue?
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".
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Site Timeline
- » FAQ 8.14 How do I modify the shadow password file on a Unix system?
- — Next thread in » PERL Discussions
- » floating point issue?
- — Previous thread in » PERL Discussions
- » s suffix question
- — Newest thread in » PERL Discussions
- » Anyone Using ESET NOD32??
- — The site's Newest Thread. Posted in » Anti-Virus Software