Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate NaN, -Infinity and +Infinity in ANSI C?

Tags:

c

nan

infinity

c89

I use ANSI C89 (not C++), and I want to generate NaN, -Infinity and +Infinity.

Is there any standard way (eg. standard macro)? Or is there any platform and compiler independent way to generate these numbers?

float f = 0.0 / 0.0; // Is f ALWAYS in any platform is NaN? 
like image 804
Amir Saniyan Avatar asked Jun 04 '11 09:06

Amir Saniyan


People also ask

Is NaN function in C?

In C, the isnan macro and the _isnan and _isnanf functions return a non-zero value if the argument x is a NAN; otherwise they return 0. In C++, the isnan template function returns true if the argument x is a NaN; otherwise it returns false .

How do you do square roots in C?

The sqrt() function is defined in math. h header file. To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator. int x = 0; double result; result = sqrt(double(x));

Which header file contains mathematical function in C?

The math. h header defines various mathematical functions and one macro. All the functions available in this library take double as an argument and return double as the result.

What is double in C example?

The range of double is 1.7E-308 to 1.7E+308. Double data can be represents in real number (1, 10), decimals (0.1, 11.002) and minus (-1, -0.00002). It can hold approximately 15 to 16 digits before and after the decimal point. For example, 4.5672, 2.45354, -5.22234, 3.12345678901, 0.15197e-7 etc.


1 Answers

There is in C99, but not in previous standards AFAIK.

In C99, you'll have NAN and INFINITY macros.

From "Mathematics <math.h>" (§7.12) section

The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; ...

If you're stuck with ANSI C89, you're out of luck. See C-FAQ 14.9.

like image 167
Mat Avatar answered Sep 21 '22 05:09

Mat