# NAN:  What is it good for?  (programming)

**URL:** <https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275>\
**Category:** Factual Questions\
**Created:** [August 11, 2011, 12:35am UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275 "2011-08-11T00:35:29Z")\
**Posts on this page:** 11\
**Page:** 3

<div class="post-metadata">

**Author:** ![septimus](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/septimus/32/410_2.png) [@septimus](https://boards.straightdope.com/u/septimus)\
**Post date:** [August 11, 2011, 4:13pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/41 "2011-08-11T16:13:37Z")

</div>

> [@engineer\_comp\_geek](#):
>
> NAN isn’t anything new. It’s been part of the IEEE 32 bit floating point standard (IEEE 754) since the late 1970s.

I’m afraid it will reveal my oldish age, but the CDC 6600 from the early 1960’s had four special floating point values (+infty, -infty, +indet, -indet) and IIRC no trapping mechanism. The numbers were somewhat “well-behaved” in that adding +infty to anything except indet or -infty resulted in +infty, the other cases indet. 0/0 resulted in indet, other divisions by zero resulted in +infty or -infty.

(With little trouble I might recall more idiosyncracies of this machine I’ve not seen for 40-odd years, even though I can’t recall yesterday’s breakfast. :smack: )

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [August 11, 2011, 4:48pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/42 "2011-08-11T16:48:24Z")

</div>

> [@Chronos](#):
>
> Instead, it quietly returns a result of NAN, or “Not a Number”, and the program continues on its merry way, propagating that NAN all over the place …

Also known as, “Ice-NAN”.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 11, 2011, 5:25pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/43 "2011-08-11T17:25:55Z")

</div>

In the specific case that prompted the question (yes, of course there was a specific case), the NANs that I was seeing ultimately came from INFs, and the INF (actually, -INF) came from a log(0). If the program had given an error of “Invalid argument for log” or something like that, then I could have gone through and checked all of the log calls in the program, and found the problem a lot more easily.

And the IEEE standard which introduced NAN might have been back in the 70s, but I certainly remember writing programs that crashed on division by zero as late as the 90s. Maybe the standard was just slow to be adopted, I don’t know.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [August 11, 2011, 5:40pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/44 "2011-08-11T17:40:32Z")

</div>

Despite the floating point specification for hardware/assembly, whether or not you get ERRDIVs is implementation dependent. For instance, see the difference in Python and Lua from the terminal on my Mac:

$python  
\>\>\> print(1/0)  
ZeroDivisionError: integer division or modulo by zero  
\>\>\> print(0/0)  
ZeroDivisionError: integer division or modulo by zero  
– Note: these are ints, so NAN doesn’t apply, right? –  
\>\>\> print(0.0/0.0)  
– Should return NAN from hardware, given specification –  
ZeroDivisionError: float division by zero  
\>\>\> print(1.0/0.0)  
ZeroDivisionError: float division by zero

$lua -i  
\>= 1/0  
inf  
\>= -1/0  
-inf  
\>= 0/0  
nan

I’ll spare you the terminal dump, but rest assured it works for +/-1.0 and 0.0 as well.

So even though the hardware specifies nan or inf be returned from the internal registers/ALU in case of nonreal or indeterminate return values, there’s still language dependency in whether or not the actual functions give you nan and inf, or if you get an error.

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [August 11, 2011, 5:54pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/45 "2011-08-11T17:54:15Z")

</div>

> [@Chronos](#):
>
> If the program had given an error of “Invalid argument for log” or something like that, then I could have gone through and checked all of the log calls in the program, and found the problem a lot more easily.

Are you programming in C on Unix, by chance? If so, you can set up your program so that floating point exceptions will generate signals, which you can catch yourself, or which will halt your program immediately (but this then is also catchable when running under a debugger).

Some sample code for this is [here](http://www.ncsa.illinois.edu/UserInfo/Resources/Hardware/CommonDoc/fpe.html#intelc).

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 11, 2011, 8:17pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/46 "2011-08-11T20:17:07Z")

</div>

> [@](#):
>
> Are you programming in C on Unix, by chance?

No such luck-- It’s FORTRAN 77.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 11, 2011, 8:42pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/47 "2011-08-11T20:42:57Z")

</div>

Is there any particular reason why you need to program in FORTRAN 77? There are a number of languages which would allow you the kind of control over how to respond to arithmetic exceptions that you desire and to which acclimating yourself would likely not be very difficult.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [August 11, 2011, 8:56pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/48 "2011-08-11T20:56:59Z")

</div>

(It may also be possible to obtain such control in FORTRAN 77; it’s just that you probably know more about the details of FORTRAN 77 than any of us do. At any rate, according to Wikipedia, floating point exception handling is at least a feature of later versions of FORTRAN)

---

<div class="post-metadata">

**Author:** ![Bytegeist](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bytegeist](https://boards.straightdope.com/u/Bytegeist)\
**Post date:** [August 11, 2011, 8:59pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/49 "2011-08-11T20:59:51Z")

</div>

[QUOTE=Chronos]  
No such luck-- It’s FORTRAN 77.

[/QUOTE]

Man oh man, you’re still on that gig?

But I would rather light a candle, than curse your darkness. Maybe one of these will help.  
[ul]  
[li][How](http://www.cisl.ucar.edu/news/01/tips/0209.fpe.html) to handle floating-point exceptions in Fortran[/li][li][Fortran](http://caligari.dartmouth.edu/doc/solaris-forte/manuals/fortran/prog_guide/6_floating.html) Programming Guide (Solaris), Chapter 6, Floating-Point Arithmetic[/li][li][Compiler](http://www.fortran-2000.com/ArnaudRecipes/CompilerTricks.html#fComp_fpe) and tools tricks, floating-point (for g77 and gfortran).[/li][/ul]  
It looks like there’s no standardized way to handle these in Fortran. Every combination of platform and compiler needs something different.

---

<div class="post-metadata">

**Author:** ![Revtim](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/revtim/32/1042_2.png) [@Revtim](https://boards.straightdope.com/u/Revtim)\
**Post date:** [August 11, 2011, 9:08pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/50 "2011-08-11T21:08:13Z")

</div>

> [@CanTak3](#):
>
> NaN is also used for an unknown/indeterminate number.

I use it in a C# .NET app I’m working on to represent unknown values. It’s a type of hardware monitoring application, and sometimes we get messages from the hw that indicate ‘unknown’ for particular values.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [August 11, 2011, 9:33pm UTC](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275/51 "2011-08-11T21:33:18Z")

</div>

> [@](#):
>
> Man oh man, you’re still on that gig?

Until I finish my thesis. It’s that, or re-write about 500 pages of code.

But I’ll take a look at those links, anyway. I’ve gotten past this particular problem (all my numbers are now numbers), but there might be other things in there I can use.

[Previous page](https://boards.straightdope.com/t/nan-what-is-it-good-for-programming/592275.md?page=2)
