cdouble

Scenarios where C returns maximum double value


I wrote a C function which returns a double value which in certain cases is returning the value

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000000000

which apparently is the largest value a double data type can store. My question is in what cases does C do this(i.e. returns the largest value), in the sense that C returns inf for 1/0 and nan for sqrt(-1).

The function involves a lot of different function calls, So I thought it would be helpful if I know what can be possibly causing this. I tried google-ing but I couldn't find a solid answer.


Solution

  • C does this when the result of an operation would overflow but current rounding mode rounds away from infinity. For example,

    double maxdouble()
    {
        fesetround(FE_TOWARDZERO);
        return(1e300*1e10);
    }
    

    returns DBL_MAX on my machine, and presumably on anything where double is the 64-bit IEEE floating point type. It returns inf if the call to fesetround is omitted or replaced with FE_TONEAREST.