mello_
mello_

Reputation: 11

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

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?

Upvotes: 1

Views: 90

Answers (1)

Amadan
Amadan

Reputation: 198556

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.

Upvotes: 0

Related Questions