Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
if I multiply 0.29 with 50, I get 14.5: Only with this line: my $num4 = sprintf ("%.64f", $num7); I get the exact result in "double" (14.999...).

The only difference between:

print 0.29 * 50;; 14.5

And:

printf "%.20f\n", 0.29 * 50;; 14.49999999999999800000

is what gets printed out; not what is calculated and stored internally.

All floating point calculations are done internally using IEEE754 semantics; if you want to see the full internal results use printf to display the results.

As for your perceived difference when gratuitously using eval:

printf "%.20f\n", eval eval( 0.29 ) * eval( 50 );; 14.50000000000000000000

The first thing to note is that the first two evals make no difference whatsoever:

printf "%.20f\n", eval 0.29 * 50;; 14.50000000000000000000

And the reason for that difference is because (in this form) eval expects a string. So the result of the multiplication (eval( 0.29 ) * eval( 50 )) is converted to a string -- using the same rules as print -- before being passed to the final eval.

You get identically different results if you do:

$n = 0.29 * 50; printf "%.20f\n", "$n";; 14.50000000000000000000

Because the internal (IEEE754) representation has been lost by the conversion to a string.

Bottom line: Stop worrying about it. Do your floating point math in the usual way (no eval's; no stringifications), secure in the full IEEE754 semantics will be used internally; and then use printf/sprintf (*only*) when you need to display them.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: IEEE-754 calculation - best way? by BrowserUk
in thread IEEE-754 calculation - best way? by Biker1971

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (7)
    As of 2014-07-29 22:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls