Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

When 1000 is less than 1000

by SavannahLion (Pilgrim)
on Sep 27, 2010 at 05:48 UTC ( #862151=perlquestion: print w/replies, xml ) Need Help??

SavannahLion has asked for the wisdom of the Perl Monks concerning the following question:

I honestly have no idea what's causing this. I can't even replicate the bug in a separate test.

In a nut shell, I am doing a set of comparisons between values in this manner (actual snippet): return ('','') if ($rsx < $sxL); where $rsx and $syH are both numbers (generally floats). In a strange bug, whenever either scalar sports the value of 1000, then the comparison above results in a TRUE!

The scalars hold values that are pretty much abused and beaten. Sometimes, I'll introduce values into the formula (this is where I get the value 1000 from), usually done if a formula can't be solved (eg divide by zero or if I want the value to specifically come out to 1000, even if the result would've been something different). In other words, if I reach that point where I'm comparing the two scalars, I know that I will sometimes be comparing two values of 1000 and I expect it not to return TRUE.

Yet, it appears to return TRUE nearly every time I end up checking if 1000 < 1000

I am truly baffled by this. Have I completely misunderstood the usage between <, > and their counterparts lt and gt? Is there some behavior difference between integers and floating numbers? Is perl misinterpreting my intent that these are indeed numbers and are treating my scalars as strings?

Some of the things I've tried:

  • Substituting lt and gt for < and >. As expected, I get unwanted behavior with the floating numbers.
  • Doing something like ((0+$rsx) < (0+$sxH)) to try and "remind" perl these are supposed to be numbers. I believe Perl factors this out so it's moot.

I am so close to stamping this bug out and moving on to the next stage of my project. :(

Replies are listed 'Best First'.
Re: When 1000 is less than 1000
by Anonymous Monk on Sep 27, 2010 at 06:31 UTC
Re: When 1000 is less than 1000
by AnomalousMonk (Bishop) on Sep 27, 2010 at 06:45 UTC
Re: When 1000 is less than 1000
by sundialsvc4 (Abbot) on Sep 27, 2010 at 14:21 UTC

    “Floating-point numbers like little piles of sand on the ground.   Each time you pick one up and move it around, you lose a little sand and you pick up a little dirt.”

    Even though the computer displays “sensible” decimal numbers to you, with so-many decimals and so forth, it actually works with base-two floating point numbers, whose internal values are almost never exactly equal to what you see.   Thus, two values which “appear” to be exactly the same, probably aren’t.   When comparing things, or when adding-up very long columns of figures, this makes all the difference.

    Therefore, you must round the numbers, or truncate them to integers or what-have-you, in order to produce the results you expect.   This statement is true for all programming languages, spreadsheets, and so forth.

    P.S.:   You will need to master the ability to explain these things, very patiently, to your friendly neighborhood accountant (or CFO).   These are people who will cheerfully spend a hundred dollars looking for one penny, and who will make your life utterly miserable while doing it.

Re: When 1000 is less than 1000
by Siddartha (Curate) on Sep 27, 2010 at 07:51 UTC
    Could you post some examples of setting values to 1000? run it through the perl debugger and see if you can find where it sets the values to 1000 and then if it is truly 1000.

      I have a confession to make.... I don't actually know how to use the Perl Debugger. :(

Re: When 1000 is less than 1000
by SavannahLion (Pilgrim) on Sep 28, 2010 at 04:00 UTC

    After reading the responses and dealing with a little downtime at work, I went through the code and finally pinpointed the problematic assignment at:

    #The assignments are condensed test values for the following formula. my ($sx1, $sy1, $sm, $rx1, $ry1, $rm) = (1000, 1000, 1.5574077246549, +1000, 1000, 0.871447982724319); my $rsx = ($sy1 - $ry1 + $rm * $rx1 - $sm * $sx1) / ($rm - $sm);

    Even though the values plugged into the formula come out to 1000, I guess that isn't the case with the floating numbers. Since I didn't want to bang my head on this forever I figured I can somewhat predict what numbers will be dumped out by the formula. My kludge is to compare the four known values with each other then jump the entire block of problematic code if they equal each other.

    I can't believe this never occurred to me. I rarely mix whole integers with floats and this is the first time I've done any comparisons. In retrospect, this is something I should have remembered from my first year in course work. Such as never using a float value to control a loop. Should have puzzled this out on my own with the realization I was comparing a float to an integer.

    Oh well, I'm not happy about the kludge, but I can live with it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://862151]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2021-02-25 19:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?