# SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

# Bug Cor 46

## Original Description

This code checks to see if a floating point value is equal to the special `Not A Number` value (e.g., if `(x == Double.NaN)`). However, because of the special semantics of NaN, no value is equal to NaN, including NaN. Thus, `x == Double.NaN` always evaluates to false. To check to see if a value contained in x is the special `Not A Number` value, use `Double.isNaN(x)` (or `Float.isNaN(x)` if x is floating point precision). – 2013-10-15

## Detailed Description

When working with floating point variables (such as `Float` and `Double`) one often wants to test if an invalid operation has been invoked. To this end one should use the `Float.isNaN(f)` function and do NOT test equality to the public constant `NaN` defined in the `Float` or `Double` class. As a matter of fact, no variable will ever be equal to the NaN constant, due to its special semantics.

NaN is just a variable defined in many programming languages to give a “meaningful” result to operations that otherwise have no mathematically defined result. The value NaN (Not a number) results from the following operations:

• Complex values
• √x where x is negative
• log(x) where x is negative
• tan(x) where x mod 180 is 90
• asin(x) or acos(x) where x is outside [-1..1]
• 0/0
• ∞/∞
• ∞/−∞
• −∞/∞
• −∞/−∞
• 0×∞
• 0×−∞
• 1∞
• ∞ + (−∞)
• (−∞) + ∞

## Sample Problem Scenario

<code Java> Float f; if (f == Float.NaN){

`System.out.println("IS NAN");`

}

Double d; if (d == Double.NaN){

`System.out.println("IS NAN");`

} </Code>

## Sample Fix

<code Java>

Float f; if (Float.isNaN(f)){

`System.out.println("IS NAN");`

}

Double d; if (Double.isNaN()){

`System.out.println("IS NAN");`

} </Code>

## Evaluation Results

Benchmark project Time # Hits Precision Recall
FB JT FB JT Delta FB JT Delta
Apache Tomcat < 1s00 100% 100% 0 100% 100% 0
Argo UML < 1s00 100% 100% 0 100% 100% 0
AWT < 1s00 100% 100% 0 100% 100% 0
Jakarta < 1s00 100% 100% 0 100% 100% 0
Java IO < 1s00 100% 100% 0 100% 100% 0
JHotDraw < 1s00 100% 100% 0 100% 100% 0
jrefactory < 1s00 100% 100% 0 100% 100% 0
JServlet < 1s00 100% 100% 0 100% 100% 0
JUnit < 1s00 100% 100% 0 100% 100% 0
Lexi < 1s00 100% 100% 0 100% 100% 0
Mapper XML < 1s00 100% 100% 0 100% 100% 0
nutch < 1s00 100% 100% 0 100% 100% 0
PMD < 1s00 100% 100% 0 100% 100% 0
quickuml < 1s00 100% 100% 0 100% 100% 0

FB = FindBugs, JT = JTransformer, Delta = JTransformer - FindBug

All measurements were taken on a machine with the following properties:

• JT Version: 4.0.0
• Eclipse Kepler
• Windows 8.1
• i5 2,30Ghz
• 8 GB Main Memory
• SSD Hardrive