How to prevent overflow when using usual math functions exp() log()?

c, c++, undefined-behavior

Solution

To expand the answer of @TheOtherGuy, you can cancel the operation if overflow occurs.

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main(void)
{
    double param, result;

    errno = 0;
    param = 1e3;
    result = exp (param);
    if (errno == ERANGE) {
        printf("exp(%f) overflows\n", param);
        result = param;
    }
    printf ("The exponential value of %f is %f.\n", param, result );
    return 0;
}

Problem

All is in the title. How to check a possible overflow when using the two functions exp() and log()?

Original source

Related problems