I'm trying to figure out what your concern might actually be.

In theory, the NV for the constant 1.1 will be the closest long double to the exact value for 1.1. As far as I can tell, the cast to double rounds to the nearest possible double value, which is almost always going to be the closest double value to the original exact value. So I don't see how you could be worried about those.

The value returned by foo(), a long double laundered through a double, is almost always going to be different from the original value - given purely random long double input, something like 99.95 % of the time. Though you'll run across cases where no loss occurs more often than that, since many interesting values are exactly representable as a double or long double, e.g. 2.5, or 1.

So, why does it matter to you that precision may be (rather, is probably going to be) lost? What do you want to do differently based on whether precision is lost?

Update: maybe all you want is to use sprintf("%.15g", perl_foo($x)) instead of just perl_foo($x)?


In reply to Re: XS, C doubles and -Duselongdouble by ysth
in thread XS, C doubles and -Duselongdouble by syphilis

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.