Thread Links Date Links
Thread Prev Thread Next Thread Index Date Prev Date Next Date Index

Re: Motion P1788/M0044:Constructors -- Voting Period begins - controlling rounding mode



On Thu, May 16, 2013 at 8:12 PM, Ian McIntosh <ianm@xxxxxxxxxx> wrote:

BK>  To do so would require a facility for rounding the text value 0.1 down into the binary format and also rounding it up.
BK>  . . .
BK>  I don't think it is readily accessible by default in common programming languages.



The 1999 version of the C standard has functions to get and set the rounding mode.  They can be called from C++ or from Fortran.  Fortran also has its own IEEE modules which could be used.

For example. in an environment that supports changing the rounding mode (as opposed to having static rounding modes), a function something like the (completely untested) following should work.

    #include <stdlib.h>
    #include <fenv.h>

    typedef struct interval_s
    {
        double lower;
        double upper;
    } interval_t;

    interval_t text2interval (const char const* text)
    {
    #pragma STDC FENV_ACCESS ON
        interval_t result;
        int original_rounding_mode = fegetround ( );
        (void) fesetround (FE_DOWNWARD);
        result.lower = atof (text);
        (void) fesetround (FE_UPWARD);
        result.upper = atof (text);
        (void) fesetround (original_rounding_mode);
        return result;
    }


The above code fragment makes a very dangerous assumption, to wit, that the atof() function from the compiler library is round-safe, which means that all of the functions it calls must also be round-safe..  In my experience few implementations make that claim.  And some of those that do make the claim are false.

A library wherein atof() is just a wrapper around a more capable function such as strtod() [or strtof() or strtold()] one often finds that the more sophisticated inner function makes assumptions about the rounding environment that may not be lightly violated.

I happen to agree that it should work.  But my opinion seems to carry little weight against the fact that for many compilers, it does not in fact work.

Do we care about the difference between that should work and what does work?  I think we should care.

Lee Winter
Nashua, New Hampshire
United States of America (RIP)