Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if a float is SNAN or QNAN

I am trying to figure out how to print out if a floating point number is QNAN or SNAN. I have already separated out the bits into the signBit exponentBit and the FractBits.

unsigned int sign = (i & 0x80000000) >> 31;
unsigned int exponent = (i & 0x7f800000) >> 23;
unsigned int fraction = (i & 0x007FFFFF);
printf("signBit %d, expBits %d, fractBits 0x%08X\n",sign, exponent, fraction);
like image 312
thepi Avatar asked Apr 16 '15 05:04

thepi


2 Answers

GNU provides a facility which was recently standardized:

Macro: int issignaling (float-type x)

Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

This macro returns a nonzero value if x is a signaling NaN (sNaN). It is based on draft TS 18661 and currently enabled as a GNU extension.

The final draft TS mentions that you might need to opt-in with a macro to get it:

#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <math.h> 
int issignaling(real-floating x);
like image 124
Potatoswatter Avatar answered Oct 08 '22 06:10

Potatoswatter


The format of the floating-point real numbers is processor-dependent. On x86 / x86-64 ISA, the top bit of the significand = 1 for quiet, 0 for signalling. (The rest of the NaN payload is still arbitrary).

  • float type:

    enter image description here

  • double type:

    enter image description here

  • long double type (not supported by Microsoft compilers):

    enter image description here

like image 20
ahmd0 Avatar answered Oct 08 '22 07:10

ahmd0