Is there a correct constant-expression, in terms of a float, for its msb?

后端 未结 3 573
孤城傲影
孤城傲影 2021-01-21 14:45

The problem: given a floating point constant expression, can we write a macro that evaluates to a constant expression whose value is a power of two equal to the most significant

3条回答
  •  离开以前
    2021-01-21 15:17

    For the sake of example, assume the type is float and let x be the input. Initially I will write this as a sequence of statements for readability, but they can be translated directly into macros that produce constant expressions.

    float y = x*(1+FLT_EPSILON)-x;
    if (y/FLT_EPSILON > x) y/=2;
    

    If we could ensure rounding-down, the initial value of y should be exactly what we want. However, if the top two bits of x are 1 and any lower bits are set, or if we hit a rounds-to-even case, x*(1+FLT_EPSILON) could exceed x by 2 units in the last place instead of just 1. I don't believe any other cases are possible, and I believe the second line accounts fully for this one.

    Written as macros:

    #define PRE_ULP(x) ((x)*(1+FLT_EPSILON)-(x))
    #define ULP(x) ((PRE_ULP(x)/FLT_EPSILON>(x) ? PRE_ULP(x)/2 : PRE_ULP(x))
    
    #define MSB_VAL(x) (ULP(x)/FLT_EPSILON)
    

提交回复
热议问题