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

Macrobian Monks,

In my script:
$a, $b, $c and $d are integers retrieved from a db query,
$e = $a*sqrt($b/$c)
$f = $d - $e

Output from my script says:
$d = 66
$e = 35.0127321335217
$f = 1451.98726786648

So I suspect I need to straighten out my integers and floating points. Best way to do this?

Thanks.

Forget that fear of gravity,
Get a little savagery in your life.

  • Comment on Odd results from mixing integer and floating point math?

Replies are listed 'Best First'.
Re: Odd results from mixing integer and floating point math?
by gargle (Chaplain) on Aug 21, 2005 at 17:03 UTC
    Could you post the exact code, please? It's a bit unclear why you end up with a result of 1451.98726786648 while your formulate indicates a simple $d - $e. In short, or 1451.98726786648 is a typo or $f = $d - $e is...
      Aie! Sometimes you just need someone else to say what you should already know. The result is so ridiculous that, yes, of course, it's a typo in the script. Had inadvertently subtracted $e from the wrong variable. That's what I get for coding on Sundays. Thanks.

      Forget that fear of gravity,
      Get a little savagery in your life.

        Would that not have been caught by use strict?

        --

        Oh Lord, won’t you burn me a Knoppix CD ?
        My friends all rate Windows, I must disagree.
        Your powers of persuasion will set them all free,
        So oh Lord, won’t you burn me a Knoppix CD ?
        (Missquoting Janis Joplin)

Re: Odd results from mixing integer and floating point math?
by tilly (Archbishop) on Aug 22, 2005 at 06:26 UTC
    Are those really your variable names?

    If so then I'd strongly recommend that you switch to more verbose and descriptive names. That might have solved this problem, because it is a lot easier to miss that you put the wrong letter somewhere than it is to miss the fact that you have the wrong name. (And a typo will result in a variable that you didn't use - which strict.pm can catch.)

    Furthermore note that $a and $b are reserved for sort, so you should definitely use something else instead of those two.