DcmbrAgnt has asked for the wisdom of the Perl Monks concerning the following question:
Can anyone tell me why:
perl -e 'print -8 + -15 * 0.5, "\n";' -15.5
does the right thing, and multiplies -15 times 0.5, then adds it (-7.5) to -8 to get -15.5, but:
print "LEQ/REQ: $leftEq[$_[0]][$i] $rightEq[$_[0]][$i]\n"; ($leftEq[$_[0]][$i], $rightEq[$_[0]][$i]) = ($leftEq[$_[0]][$i] + $leftStorDelta[$i] * $factor, $rightEq[$_[0]][$i] + $rightStorDelta[$i] * $factor); print "Fct: $factor Delt: $leftStorDelta[$i] $rightStorDelta[$i]\n"; print "Post-LEQ/REQ: $leftEq[$_[0]][$i] $rightEq[$_[0]][$i]\n\n";
gives output like this:
LEQ/REQ: -8 -8 Fct: 0.50 Delt: -15 -15 Post-LEQ/REQ: -8 -8
even though it should be the same darn thing. From what I can gather, it's treating $factor (0.5) like an integer, multiplying -8 by 1.
...but why?
It should be noted that I only need two decimal places of precision in the answer, so if there's a way I can avoid the whole floating point jungle and just deal with decimal numbers of a form \d.\d\d, that would be more than fine.
My main question is why I'm not getting any decimal precision at all when I multiply times $factor, yet it works just fine with constants in a 'perl -e' one-liner.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Floating point hell
by GrandFather (Saint) on Dec 19, 2015 at 19:58 UTC | |
|
Re: Floating point hell
by AnomalousMonk (Archbishop) on Dec 19, 2015 at 22:43 UTC | |
|
Re: Floating point hell
by Apero (Scribe) on Dec 19, 2015 at 19:55 UTC | |
|
Re: Floating point hell
by Anonymous Monk on Dec 19, 2015 at 19:38 UTC |