Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

C#: Never Test For NaN With Equality Test

Rate me:
Please Sign up or sign in to vote.
4.56/5 (15 votes)
15 Jan 2020CPOL 18K   49   9   15
Never compare NaN with itself!

Let's write a simple program to test. To get a NaN, we do a 0/0 and store it in num. First of all, the divisor has to be a float-point type because DivideByZeroException shall be thrown for integer or decimal zero divisor. Note: Non-zero number divided by zero (float) gives an infinity number. In our program, we compare num to NaN and next, compare num to itself and then, we compare 2.0 to NaN. Lastly, we check if num is NaN with Double.IsNaN().

C#
double num = 0.0 / 0.0; // Result of 0 divided by 0 is a NaN

Console.WriteLine("num == NaN is {0}", (num == double.NaN));
Console.WriteLine("num == num is {0}", (num == num));
Console.WriteLine("2.0 == NaN is {0}", (2.0 == double.NaN));
Console.WriteLine("double.IsNaN(num) is {0}", double.IsNaN(num));

This is the output. All the 3 NaN comparisons return False while IsNaN() check on num returns True. This is exactly the same behaviour with C++. The advice for this tip is never test for NaN with equality test, use IsNaN() instead.

num == NaN is False
num == num is False
2.0 == NaN is False
double.IsNaN(num) is True

If you are interested to know more about floating-point format, read my Succinct Guide to Floating Point Format For C++ and C# Programmers.

History

  • 16th January, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
QuestionNaN as a singleton object ... not a value ... per Type ? Pin
BillWoodruff21-Jan-20 22:53
professionalBillWoodruff21-Jan-20 22:53 
AnswerRe: NaN as a singleton object ... not a value ... per Type ? Pin
Shao Voon Wong22-Jan-20 0:18
mvaShao Voon Wong22-Jan-20 0:18 
GeneralRe: NaN as a singleton object ... not a value ... per Type ? Pin
BillWoodruff22-Jan-20 1:28
professionalBillWoodruff22-Jan-20 1:28 
GeneralRe: NaN as a singleton object ... not a value ... per Type ? Pin
Shao Voon Wong22-Jan-20 1:45
mvaShao Voon Wong22-Jan-20 1:45 
GeneralMy vote of 1 Pin
BillWoodruff21-Jan-20 22:42
professionalBillWoodruff21-Jan-20 22:42 
GeneralRe: My vote of 1 Pin
Shao Voon Wong22-Jan-20 0:07
mvaShao Voon Wong22-Jan-20 0:07 
GeneralRe: My vote of 1 Pin
BillWoodruff22-Jan-20 1:31
professionalBillWoodruff22-Jan-20 1:31 
GeneralRe: My vote of 1 Pin
Shao Voon Wong22-Jan-20 1:43
mvaShao Voon Wong22-Jan-20 1:43 
GeneralMy vote of 3 Pin
r_alf18-Jan-20 4:25
r_alf18-Jan-20 4:25 
GeneralRe: My vote of 3 Pin
Shao Voon Wong22-Jan-20 0:14
mvaShao Voon Wong22-Jan-20 0:14 
GeneralMy vote of 5 Pin
englebart17-Jan-20 8:59
professionalenglebart17-Jan-20 8:59 
QuestionWrong conclusion Pin
ktistakis17-Jan-20 8:33
ktistakis17-Jan-20 8:33 
GeneralMy vote of 2 Pin
Member 1356338517-Jan-20 7:11
Member 1356338517-Jan-20 7:11 
GeneralRe: My vote of 2 Pin
Shao Voon Wong22-Jan-20 0:25
mvaShao Voon Wong22-Jan-20 0:25 
QuestionThe really interesting thing ... PinPopular
W. Kleinschmit17-Jan-20 2:56
W. Kleinschmit17-Jan-20 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.