[Cfp-interest] functions - inexact for floating to integer conversion

Jim Thomas jwthomas at cup.hp.com
Thu Jul 22 10:13:10 PDT 2010


At the last CFP Study Group meeting Fred pointed out that the proposed 
sequence for getting the inexact exception for floating to integer 
conversions was flawed because it would sometimes signal inexact (as 
well as invalid) in invalid cases. The following example shows a fix, at 
the cost of a promotion and a test:

#include <stdint.h>
#include <math.h>
#include <fenv.h>
uint16_t u16ceilx(double x) {
        #pragma STDC FENV_ACCESS ON
        double xi;
        uint16_t r;
        xi = ceil(x);
        r = xi;
        if (islessgreater(xi, x) && (r == xi))
                feraiseexcept(FE_INEXACT);
        return r;
}

-Jim



More information about the Cfp-interest mailing list