Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


I have a need to control the rounding direction being used for floating point calculations. In C I would just call fegetround() and fesetround(). I have used Inline::C with some success, but I wonder if there isn't a better way to accomplish the same thing.

Is there some magic flag or module that will let me manipulate the rounding mode? I think my options are:

  1. Keep using Inline::C
  2. Write a new module in C to expose the functions defined in fenv.h
  3. ...

I've included a small program below that shows my current solution. This works with Strawberry Perl although I have yet to test it anywhere else so YMMV.

Sample Code

#!/usr/bin/perl use warnings; use strict; use Inline C => <<'END_C'; #include <fenv.h> int _getround() { return fegetround(); } int _setround(int mode) { return fesetround(mode); } int _round_down() { return fesetround(FE_DOWNWARD); } END_C ## simulate my object my $one = 1.0; my $three = 3.0; ## save the rounding mode and set to round towards negative infinity my $mode = _getround(); my $changed = _round_down(); ## perform the calculations my $lb = $one / $three; my $ub = -( -$one / $three ); ## restore the original rounding mode to be nice _setround($mode); ## display the results printf( "\$lb = %.23f\n", $lb ); printf( "\$ub = %.23f\n", $ub ); print "\$lb == \$ub ", $lb == $ub ? 'true' : 'false', "\n"; print "\$changed = $changed\n";


Sample Output

P:\>fround $mode = 0 $lb = 0.33333333333333331000000 $ub = 0.33333333333333337000000 $lb == $ub false $changed = 0

Owl looked at him, and wondered whether to push him off the tree; but, feeling that he could always do it afterwards, he tried once more to find out what they were talking about.

In reply to Is there a better way to call fe(get|set)round? by HollyKing

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (5)
    As of 2017-12-11 18:23 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (310 votes). Check out past polls.