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.


In reply to Floating point hell by DcmbrAgnt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.