Java variable shows value of 'NaN' but var == Double.NaN is false, why? -


i have code below:

sim = top / bottom;  if(sim == double.nan) {     system.out.println("sim nan"); } else {     system.out.println(sim); } 

for values in program, top / bottom 0.0 / 0.0 should equal double.nan when stop @ breakpoint after section output shows 'nan' -i.e. value of sim rather 'sim nan'.

why test sim == double.nan return false if see 'nan' in debugger?

you can check nan via double.isnan():

if (double.isnan(sim)) {   // nan } else {   // not nan } 

on first thoughts not intiutive, have know it. reason behind it, specified ieee floating point standard.

you can argument: nan not number, can not number, 2 nan cannot compared equal.
example = 0.0 / 0.0 nan; , sqrt(-1) nan, too. values cannot compared same.

additional reading: http://en.wikipedia.org/wiki/nan


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -