When you say 0.98 to Perl, it creates a "double" that has one of the closest possible values to 0.98. That isn't quite exactly 0.98, but it is pretty darn close. Close enough that if you asked Perl to convert this value back into a string, it would probably just give you "0.98".

But you stored that value into a "single" in the database. Well, that got you one of the closest values to 0.98 that can fit into a "single". When you pull that back out of the database, Perl stores it into a "double" and later you ask it to convert it to a string.

Well, Perl can tell that the number is close to 0.98 but not nearly as close as is possible when using a "double", so Perl doesn't just stop at "0.98".

You see, when you convert 0.98 to a "double", you get something that is closer to 0.98 than 0.980000000000001 is. But in a "single", the closest you can get is either 0.979999542236328 or 0.980000019073486.

So you need to grab only about the first 7 significant digits when converting a single to a string. Something like: $num= 0 + sprintf "%.6e", $num;

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Filthy Floats by tye
in thread Filthy Floats by THuG

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.