Home > Archive > PERL Miscellaneous > November 2005 > Perl subtraction wierdness
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Perl subtraction wierdness
|
|
| jai_guevara@yahoo.com 2005-11-28, 3:56 am |
| I'm finding some simple subtractions that result in negative numbers
give wierd results. I know I could use sprintf to format it, but
shouldn't this just work?
> perl -e 'print 17.50-18.30'
> -0.800000000000001
> perl -e 'print 18.00-18.30'
> -0.300000000000001
My perl version is v5.8.6 built for i586-linux-thread-multi.
| |
| Brian Wakem 2005-11-28, 3:56 am |
| jai_guevara@yahoo.com wrote:
> I'm finding some simple subtractions that result in negative numbers
> give wierd results. I know I could use sprintf to format it, but
> shouldn't this just work?
>
>
> My perl version is v5.8.6 built for i586-linux-thread-multi.
perldoc -q numbers
--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
| |
| Dr.Ruud 2005-11-28, 7:56 am |
| jai_guevara@yahoo.com:
[color=darkred]
> I'm finding some simple subtractions that result in negative numbers
> give wierd results. I know I could use sprintf to format it, but
> shouldn't this just work?
>
What do you consider weird (or 'wierd' as you spell it) about it?
--
Affijn, Ruud
"Gewoon is een tijger."
| |
| Tad McClellan 2005-11-28, 7:56 am |
| jai_guevara@yahoo.com <jai_guevara@yahoo.com> wrote:[color=darkred]
> I'm finding some simple subtractions that result in negative numbers
> give wierd results. I know I could use sprintf to format it, but
> shouldn't this just work?
>
That isn't weird, that is a consequence of computers working
on floating-point numbers represented in base 2 (binary).
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 SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| Eric J. Roode 2005-11-29, 3:57 am |
| jai_guevara@yahoo.com wrote in news:1133169175.250621.308790
@f14g2000cwb.googlegroups.com:
> I'm finding some simple subtractions that result in negative numbers
> give wierd results. I know I could use sprintf to format it, but
> shouldn't this just work?
>
>
> My perl version is v5.8.6 built for i586-linux-thread-multi.
>
I swear, they should teach kids about assembly language, registers, and
data representations. Why, back in my day....
</old-person-rant>
Okay. Duuuude. You think computers have infinite precision? No, that
would require infinite memory. Ah, but I hear you say, "17.50 doesn't
require infinite precision, just two decimal places! Same with 18.30!"
Right. *Decimal places*. Computers do computations in *binary*. The
numbers 17.50 and 18.30 simply *cannot* be represented exactly in the
commonly-used binary floating-point representations used in most modern
computers. Suck it up. (actually, I lied. 17.50 *can* be represented
exactly. But not 18.30).
You cannot expect 17.50 plus 0.80 to equal 18.30. You have to allow for
this. (Yeah, you'd expect a modern proglang like Perl to account for
this, but it doesn't. Oh well.)
Read the FAQ; also read up on data representation of floating-point
numbers.
--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
| |
| jai_guevara@yahoo.com 2005-11-29, 3:57 am |
| Thanks for the replies. I can't believe I haven't seen this before. It
makes sense, but it's still weird for a language like Perl. I usually
expect a higher-level programming language to insulate me from the
gritty details of binary arithmetic.
Also, please excuse the weird spelling. What happened to "I before E
except after C"? I must have skipped the day they covered exceptions.
| |
| robic0 2005-11-29, 3:57 am |
| On 28 Nov 2005 21:57:00 -0800, jai_guevara@yahoo.com wrote:
>Thanks for the replies. I can't believe I haven't seen this before. It
>makes sense, but it's still weird for a language like Perl. I usually
>expect a higher-level programming language to insulate me from the
>gritty details of binary arithmetic.
>
>Also, please excuse the weird spelling. What happened to "I before E
>except after C"? I must have skipped the day they covered exceptions.
Except for weird, because wierd is too dyslexic for most and looks
like "wire". But thats English.
| |
| Tad McClellan 2005-11-29, 7:57 am |
| jai_guevara@yahoo.com <jai_guevara@yahoo.com> wrote:
> I usually
> expect a higher-level programming language to insulate me from the
> gritty details of binary arithmetic.
How did you come to have that expectation?
Have you used a few languages that did that for you?
Could you name an example to help enlighten us?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
| |
| poopdeville@gmail.com 2005-11-29, 6:59 pm |
|
Tad McClellan wrote:
> jai_guevara@yahoo.com <jai_guevara@yahoo.com> wrote:
>
>
>
> How did you come to have that expectation?
>
> Have you used a few languages that did that for you?
>
> Could you name an example to help enlighten us?
>
Mathematica does it. It obviously can't do it in every case, but it
handles all the cases I've asked it to.
'cid 'ooh
| |
| jai_guevara@yahoo.com 2005-11-30, 3:58 am |
| Sure. This problem got me to thinking, so here are some examples.
perl -e "print 17.50-18.30"
-0.800000000000001
awk 'BEGIN {print 17.50-18.30}'
-0.8
python -c "print 17.50-18.30"
-0.8
t.c: main() { printf("%f\n", 17.50-18.30); }
cc t.c; a.out
-0.800000
mysql -Ne "select 17.50-18.30"
+-------+
| -0.80 |
+-------+
| |
| Dr.Ruud 2005-11-30, 3:58 am |
| jai_guevara@yahoo.com:
> Sure. This problem got me to thinking, so here are some examples.
>
> perl -e "print 17.50-18.30"
> -0.800000000000001
That just shows that the DBL_DIG macro from float.h returned 15.
See perldoc perlvar, look for "printed numbers".
$ perl -e '$#="%.6g"; print(17.5-18.3)'
-0.8
$ perl -e '$#="%.20g"; print(17.5-18.3)'
-0.80000000000000071054
$ perl -e 'print(1/3)'
0.333333333333333
$ perl -e '$#="%.99g"; print(1/3)'
0. 3333333333333333148296162562473909929394
72198486328125
--
Affijn, Ruud
"Gewoon is een tijger."
|
|
|
|
|