Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Why does perl math suck?

by MidLifeXis (Monsignor)
on Jan 12, 2011 at 14:12 UTC ( [id://881895]=note: print w/replies, xml ) Need Help??


in reply to Why does perl math suck?

An otherwise interesting question that comes up periodically, but -- for the logic behind the question: "Why are you still beating your wife?" Assumption is made that Perl (or any other language using floats) is doing the wrong thing instead of assuming that something is wrong, but you don't know what.

Update: sundialsvc4 is correct that the original is presented as "Have you stopped beating your wife?", but there is a slightly different twist on that presented by the OP - a direct accusation that Perl is currently doing something wrong, not that in the past, Perl has done something wrong. Therefore my change on the original.

--MidLifeXis

Replies are listed 'Best First'.
Re^2: Why does perl math suck?
by sundialsvc4 (Abbot) on Jan 12, 2011 at 14:51 UTC

    IIRC, the fallacy was stated, “have you stopped beating your wife?”

    Well....?

    One thing that is taught very early on in engineering schools is exactly how many of those digits on your pocket calculator are actually “significant.”   I remember a friend of mine explaining why, on a particularly difficult exam, the challenge of one question was how to arrive at the answer using no more than three consecutive math operations.   (Within four operations, apparently, all of the significance of the answer would have been lost.)   I nodded politely, but blankly ... and thanked my lucky stars that I didn’t go to Georgia Tech.   ;-)

      The order in which the operations are done matter more than the number of operations. Using a/d + b/d + c/d instead of (a + b + c)/d can yield different results, and not because of the number of operations.

        A more specific and illustrative example is $small+$huge1-$huge2 where ($small+$huge1)-$huge2 is easily much less accurate than $small+($huge1-$huge2) (where parens imply order of evaluation, which isn't always the case).

        For example:

        #!/usr/bin/perl -lw use strict; my $h1= my $h2= 1e20; my $s= 1.2345; print $s+$h1-$h2; print $s+($h1-$h2); __END__ 0 1.2345

        (I'd actually be surprised to see an example where the difference you proposed, ($a+$b+$c)/$d vs $a/$d+$b/$d+$c/$d, made more than a couple of least-significant bits of difference in the result -- though that would be enough to thwart the use of ==, of course.)

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://881895]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found