Tuesday 5 August 2014

NAN(Not A NUMBER) and Infinity Concept in Double and Float Classes

NAN -->


NAN is applicable for only Double and Float classes

In integral Arithmetic there is no way to represent undefined result
but in floating point arithmetic there is way to represent such kind of result.

//Simple example to demonstrate above concept


public class JavaApplication4 {

   public static void main(String[] args) {
        // TODO code application logic here
    double a=0.0;
    double b=0.0;
        System.out.println("Result="+a/b);
        }
}

O/p:

Result=NaN

Infinity-

  Infinity is applicable for only Double and Float classes

In integral Arithmetic there is no way to represent divide by zero condition
but in floating point arithmetic there is way to represent such kind of result
.
.
//Simple example to demonstrate above concept

public class JavaApplication4 {

   public static void main(String[] args) {
        // TODO code application logic here
    double a=10.0;
    double b=0.0;
        System.out.println("Result="+a/b);
        }
}

 O/p:
Result=Infinity



You can see the static final variable available in the class by using following way


C:\Users\Kunal>javap java.lang.Float
Compiled from "Float.java"
public final class java.lang.Float extends java.lang.Number implements java.lang
.Comparable<java.lang.Float> {
  public static final float POSITIVE_INFINITY;
  public static final float NEGATIVE_INFINITY;
  public static final flaoat nan;
 


No comments:

Post a Comment