What is NaN in C programming
How do you use nan and inf in C?
I have a numeric method that can return nan or inf if an error has occurred, and for testing purposes I want to temporarily force it to return nan or inf to make sure the situation is handled correctly. Is there a reliable compiler-independent Ability to create values for nan and inf in C?
After Googling for about 10 minutes, I was only able to find compiler dependent solutions.
You can test if your implementation has it:
The existence of is guaranteed by C99 (or at least the latest draft) and "expands to a constant expression of type float representing positive or unsigned infinity if available; otherwise to a positive constant of type float, which overflows at compile time . "
may or may not be defined, and "is defined if and only if the implementation supports quiet NaNs for the float type. It is expanded to a constant expression of type float representing a quiet NaN."
When comparing floating point values, keep the following in mind and do the following:
is wrong. One way to look for NaN would be:
You can also do the following: To test for NaN.
There are also,,, and macros in in C99.
C99 also has functions:
Docs INFINITY Docs NAN
There is no compiler-independent method because neither the C (nor the C ++) standards state that the floating point math types must support NAN or INF.
To edit: I just checked the wording of the C ++ standard and it says these functions (members of the numeric_limits template class):
We will return NAN representations "if available". It is not extended, which means "if available", but presumably "if the FP representative of the implementation supports it". There is also a function:
This returns a positive INF representative "if available".
These are both defined in the header - I would guess the C standard has something similar (probably also "if available"), but I don't have a copy of the current C99 standard.
This works for both and:
Edit: As someone said, the old IEEE standard said such values should throw traps. However, the new compilers almost always turn off the traps and return the specified values because the trapping interferes with error handling.
A compiler independent way, but not a processor independent way to get this:
This should work on any processor using the IEEE 754 floating point format (which x86 does).
UPDATE: Tested and updated.
I'm also surprised that these aren't compile-time constants. But I suppose you could create these values easily enough simply by executing a statement that returns such an invalid result. Divide by 0, log of 0, tan of 90, something like that.
I usually use
This works at least in IEEE 754 contexts, since the highest double value that can be represented is approximate. would work as well as would, but three nines are enough and memorable. Since this is either a double literal (in the case) or an actual value, it remains infinite even if you use 128-bit ("long double") floats.
Here's an easy way to define these constants, and I'm pretty sure they're portable:
When I run this code:
- Why are some people so personable
- What are dormant memories
- What is port forwarding
- What is bibliophilism
- What does DoorDarshan do to modernize itself
- What are some probability research topics
- Fraud can happen on the stock exchange
- Are the brains of creative people biologically different
- Does Islam believe in astrology
- What do eBay employees think of Amazon
- What's wrong with the Steelers
- Who delivers Amazon's Prime Pantry Boxes orders
- REAL ESTATE MUMBAI 2
- Why is therapy advice overrated?
- What made Ivan the Terrible so terrible
- Why is Owaisi famous in Hyderabad
- What is the correct name for Panadol
- Experience unsymbolized thinking
- Who uses Lynda com
- Dose of peanut butter increases testosterone
- The gate check is easy
- What drives a senior software engineer crazy
- How good is the Changi General Hospital
- Do psychopaths identify with a gender
- A writing spider is considered poisonous
- How European your country feels
- How can I delete a Justdial account?
- What is an electron accelerator
- Can a star be tide locked?
- Does dissection matter in anatomy
- What are transverse and longitudinal sound waves
- How to mix primer with paint
- What happened to eve com