NAN & # 8594; to distinguish between division by zero and an indicator with a very large negative value

I have included validation for nan in my testuite.

SIGFPE is now thrown on the next line

const double retVal =exp(exponent); 

The indicator has a value of approximately 4000. This, of course, is very close to zero and without nan-verification, the calculation continues with zero.

The SIGFPE signal is now thrown. I assume this is a denormal number (represented as NAN).

How can I distinguish between division by zero and exponent with a very large negative value?

In the second case, further calculations with zero work in my case, since I do not need to distinguish between +0 and -0.

EDIT: I tried what @ user2079303 suggested. My code is as follows:

#include <signal.h>
void fpe_handler(int signo, siginfo_t *info, void *context) {
    switch(info->si_code) {
        case FPE_INTDIV: // division by zero
            break;
        case FPE_FLTUND: // underflow
            break;
        // other cases ...
    }
}

int main() {
    struct sigaction action = {};
    action.sa_flags     = SA_SIGINFO;
    action.sa_sigaction = fpe_handler;
    sigaction(SIGFPE, &action, NULL);
    const double a = 0.0/0.0;
    if (a>0) 
        return 0;
    else
        return 1;
}

, .

+4
1

, ++, undefined, .


IEEE 754 . , <cfenv>:

std::feclearexcept(FE_ALL_EXCEPT);
// do calculation here
if(std::fetestexcept(FE_DIVBYZERO)) {
    // I was just divided by zero. The sky is falling.
    std::abort();
} else if(std::fetestexcept(FE_UNDERFLOW)) {
    // I just underflowed. This is my life now.
}

, , feenableexcept ( GNU, ).

SIGFPE ++. POSIX:

void fpe_handler(int signo, siginfo_t *info, void *context)
{
    switch(info->si_code) {
        case FPE_FLTDIV: // division by zero
            break;
        case FPE_FLTUND: // underflow
            break;
        // other cases ...
    }
}

// registering
struct sigaction action = {};
action.sa_flags     = SA_SIGINFO;
action.sa_sigaction = fpe_handler;
sigaction(SIGFPE, &action, NULL);
+3

Source: https://habr.com/ru/post/1685585/


All Articles