Monday 17 October 2011

why there is a problem in NAN { Not a Number } with Java .

If you are new to java than it really helps you because there are somethings which generally looks like error and exceptions and they are . but do you ever think why this error or exception is coming in our program . 


Have you ever deal with NAN { not a number } :- you can say that this is a problem in java if you are "new to java" but if you are learning java from 3-4 months and now you are Java lover like me than you have to read pdf " JAVAHURTS " and when you complete this book after it you can easily say that NAN is created for performance and for user friendly .


generally "if else " is used by all programmer in this world . because i think "if else" is the basic thing for dividing the way . and when we use 


----------------------------
if( ----- )
{
}
else 
{
}
----------------------------


for all of us if is only this , but do you ever tried for which type of things are performed inside machine for implementing the " if else " loop . 


if-else in java performs too many things , there can be two type of things which are related with if-else loop .
so the dividing means comparision is done with some operator like 
 > , == , < , != , >= , =< .
so for me the comparision can be performed between two things or comparision can be performed between only one thing .


: - so there are two things 
-----------------------------------
if( one operand and one operator )
{
}
else
{
}
-----------------------------------
if( two operand and one operator )
{
}
else
{
}
-----------------------------------------------------------------


so different different things are performed between these two .
, lets start with " one operand and one operator "
so in it we have one value . and the comparision is peformed with 0 like


if(n == 0)//( n != 0) etc ...
{
}
else
{
}


so inside this type of code 


Conditional branch: Integer comparison with zero
OpcodeOperand(s)Description
ifeqbranchbyte1, branchbyte2pop int value, if value == 0, branch to offset
ifnebranchbyte1, branchbyte2pop int value, if value != 0, branch to offset
ifltbranchbyte1, branchbyte2pop int value, if value < 0, branch to offset
iflebranchbyte1, branchbyte2pop int value, if value <= 0, branch to offset
ifgtbranchbyte1, branchbyte2pop int value, if value > 0, branch to offset
ifgebranchbyte1, branchbyte2pop int value, if value >= 0, branch to offset



-----------------------------------------------------------------
lets start with " two operand and one operator "
here if-else is performed like


if( a > b ) // ( a == b ) etc ...
{
}
else
{
}




Conditional branch: Comparison of two integers
OpcodeOperand(s)Description
if_icmpeqbranchbyte1, branchbyte2pop int value2 and value1, if value1 == value2, branch to offset
if_icmpnebranchbyte1, branchbyte2pop int value2 and value1, if value1 != value2, branch to offset
if_icmpltbranchbyte1, branchbyte2pop int value2 and value1, if value1 < value2, branch to offset
if_icmplebranchbyte1, branchbyte2pop int value2 and value1, if value1 <= value2, branch to offset
if_icmpgtbranchbyte1, branchbyte2pop int value2 and value1, if value1 > value2, branch to offset
if_icmpgebranchbyte1, branchbyte2pop int value2 and value1, if value1 >= value2, branch to offset





now the topic " because we are cleared with all if-else conditions " 
so NAN in java is occurred  when we deal with floating point . now . perform a operation in if else 


 if (  Math.sqrt(-1) == Math.sqrt(-1) )
 {
 }
 else 
 {
 }


if you are new than you will say yes is the answer means if is followed . but why else is followed in this condition .
so the whole thing is lie in the floating point .


floating point comparision used 2 instruction which are *fcmpg* and *fcmpl* . so if you are doing operation or comparision between two simple float than they both will hold same value means result will occur when they hold same value , means both fcmpg and fcmpl instructions push a 0 if the values are equal, a 1 if the value1 is greater than value2, and a -1 if value1 is less than value2. But if one or both of the values is NaN, the fcmpg instruction pushes a 1, whereas the fcmpl instruction pushes a -1.


but we have a condition where the two instruction will hold different values when any instruction have NAN so thats the reason behind else is followed .
                                          thanks tushar .

No comments: