Hello, I'm trying to calculate some values in the IEEE 754 format, incl. the restrictions of accuracy. For example, if I multiply 0.29 with 50, I get 14.5:
my $num1 = 0.29; my $num2 = 50; my $num3 = $num1 * $num2; print "num3=$num3\n"; # is '14.5'
Only with this line:
my $num4 = sprintf ("%.64f", $num7);
I get the exact result in "double" (14.999...). Is there are way to enable to get the same result without the sprintf instruction? Also if I use "eval", the result differs from the IEEE-754 calculation. What I want to do is to calculate everything with IEEE-754 numbers ("double" in C).
my $num1 = 0.29; my $num2 = sprintf ("%.64f", $num1); my $num3 = 50; my $num4 = sprintf ("%.64f", $num3); my $num5 = $num2 * $num4; my $num6 = sprintf ("%.64f", $num5); my $num7 = $num1 * $num3; my $num8 = sprintf ("%.64f", $num7); my $num9 = eval(eval($num1) * eval($num3)); my $num10 = sprintf ("%.64f", $num9); results in num1=0.29 num2=0.2899999999999999800159855567471822723746299743652343750000... num3=50 num4=50.000000000000000000000000000000000000000000000000000000000... num5=14.5 num6=14.499999999999998223643160599749535322189331054687500000000... num7=14.5 num8=14.499999999999998223643160599749535322189331054687500000000... num9=14.5 num10=14.50000000000000000000000000000000000000000000000000000000...

In reply to 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!
  • 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.