26 lines
626 B
C
26 lines
626 B
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <math.h>
|
|
|
|
int is_nan_custom(float f) {
|
|
uint32_t bits;
|
|
memcpy(&bits, &f, sizeof(float));
|
|
|
|
uint32_t exp = (bits >> 23) & 0xFF;
|
|
uint32_t mantissa = bits & 0x7FFFFF;
|
|
|
|
return (exp == 0xFF) && (mantissa != 0);
|
|
}
|
|
|
|
int main() {
|
|
float a = 0.0f / 0.0f;
|
|
float b = 1.0f / 0.0f;
|
|
float c = 3.14f;
|
|
|
|
printf("0.0/0.0 is NaN: %d\n", is_nan_custom(a)); // 1 (true)
|
|
printf("1.0/0.0 is NaN: %d\n", is_nan_custom(b)); // 0 (false)
|
|
printf("3.14 is NaN: %d\n", is_nan_custom(c)); // 0 (false)
|
|
|
|
return 0;
|
|
} |