How to check if a floating point exception happened using Go?

68 views Asked by At

I'm trying to create a program that receives an expression with floating point numbers and shows if any exceptions occurred.

I know that if I wrote it in C, it would be like this:

#include <stdio.h>
#include <fenv.h>

//floating point math happens before calling the function
void displayExceptions() {
    printf("Exception FE_DIVBYZERO: %d\n", fetestexcept(FE_DIVBYZERO));
    printf("Exception FE_INEXACT: %d\n", fetestexcept(FE_INEXACT));
    printf("Exception FE_INVALID: %d\n", fetestexcept(FE_INVALID));
    printf("Exception FE_OVERFLOW: %d\n", fetestexcept(FE_OVERFLOW));
    printf("Exception FE_UNDERFLOW: %d\n", fetestexcept(FE_UNDERFLOW));
}

Is there a way to check those exceptions in go?

1

There are 1 answers

0
Amadan On

AFAIK, no; however many of them will produce NaN, Inf or 0.0, which can be tested for using math.IsNaN(x), math.IsInf(x) and x == 0.0.