Hi,
The demo script (named 'double.pl'):
use warnings; use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; SV * foo(SV * nv) { double x = (double)SvNV(nv); return newSVnv(x); } EOC print perl_foo(2.5), "\n"; print perl_foo(1.1), "\n"; # LINE 17 sub perl_foo { my $ret = foo($_[0]); if($_[0] - $ret) {warn "Loss of precision"} # LINE 21 return $ret; }
That script does pretty much what I want. On a perl built without long double support it simply prints out:
2.5 1.1
On a (linux, mdk-9.1) perl built with long double support it prints out (transcribed, not copy'n'pasted):
2.5 Loss of precision at double.pl line 21 1.10000000000000009
The key points to the exercise are:

1) That the warning is emitted for perl_foo(1.1) but not for perl_foo(2.5) - ie I want to see the warning on the -Duselongdouble build of perl only when there's an actual loss of precision;

2) The detection of the loss of precision is being handled by the perl code and not by the C code (over which, in the real life situation, I have no control);

3) The warning should never be seen on a perl that was not built with long double support.

The questions:

1) The warning is reported as coming from line 21. I would prefer to see line 17 reported as the line that generated the warning. How do I go about achieving that ? (I ask that question with a pained expression on my face, for I feel I should know the answer - or be able to find it for myself.)

2) Is simply subtracting the return value from the argument a reliable way of testing whether precision has been lost ? (It seems obvious to me that the answer to that question is "yes" ... but I'm always wary of the validity of answers that seem obvious :-)

3) Is there a better way that a -Duselongdouble build of perl could determine that foo(1.1) would lead to a loss of precision ? (It's only a minor issue, but I would prefer that a -Duselongdouble build of perl could tell in advance that foo(1.1) was going to lead to a loss of precision, rather than having to wait until foo(1.1) had executed.)

Cheers,
Rob

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