Deal with NaN double value

In this chapter you will learn:

  1. What is double type NaN(Not a Number)

double NaN

Dividing 0.0 by 0.0 returns NaN. square root of a negative number is NaN. For example, System.out.println(0.0/0.0) and System.out.println(Math.sqrt(-1.0)) output NaN.

Dividing a positive number by +infinity outputs +0.0. For example, System.out.println(1.0/(1.0/0.0)); outputs +0.0.

Dividing a negative number by +infinity outputs -0.0. For example, System.out.println(-1.0/(1.0/0.0)); outputs -0.0.

public class Main {
  public static void main(String[] args) {
    Double d1 = new Double(+0.0);
    System.out.println(d1.doubleValue());
    //ja v  a2  s  .c o m
    Double d2 = new Double(-0.0);
    System.out.println(d2.doubleValue());
    System.out.println(d1.equals(d2));
    System.out.println(+0.0 == -0.0);

    
  }
}

The following code parses command-line arguments into double precision floating-point values

public class Main {
  public static void main(String[] args) {
    if (args.length != 3) {
      System.err.println("usage: java Main value1 op value2");
      System.err.println("op is one of +, -, *, or /");
      return;/*from   j  a v a 2s.  co m*/
    }
    try {
      double value1 = Double.parseDouble(args[0]);
      double value2 = Double.parseDouble(args[2]);
      if (args[1].equals("+")) {
        System.out.println(value1 + value2);
      } else if (args[1].equals("-")) {
        System.out.println(value1 - value2);
      } else if (args[1].equals("*")) {
        System.out.println(value1 * value2);
      } else if (args[1].equals("/")) {
        System.out.println(value1 / value2);
      } else {
        System.err.println("invalid operator: " + args[1]);
      }
    } catch (Exception nfe) {
      System.err.println("Bad number format: " + nfe.getMessage());
    }
  }
}

The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.

Double class provides several methods for converting a double to a String and a String to a double.

Next chapter...

What you will learn in the next chapter:

  1. How to compare two double values
Home » Java Tutorial » Primitive Data Types

Introduction

    Java Primitive Data Types

Boolean

    Java boolean type
    Java boolean type conversion
    Convert string value to boolean
    Convert boolean to string

Char

    Java char type
    Compare two char values
    Change char case
    Java char conversion
    Java char value and its attributes

Byte

    Java byte type
    Convert Byte to String
    Convert String to byte
    Byte object constructor
    Byte's max and min value
    Compare two byte values
    Convert Byte to byte, double, float, int, long and short

Short

    Java short type
    Short min/max value and size
    Create Short object
    Compare short values
    Convert short to String
    Convert Short to primitive types
    Convert string to short
    Reverse bytes

Integer

    Java int type
    int max/min value
    Create Java integer
    Convert int to binary, hexadecimal and octal format
    Compare integer values
    Integer sign
    Convert string to int
    Convert int to primitive types
    Convert int to String
    int bit operations

Long

    Java long type
    Compare two long values
    Convert long to binary, hex and octal
    Convert long value to primitive types
    Convert String to long value
    Convert long to String

Float

    Java float type
    Java float type conversion
    Predefined value for float type
    Compare two float value

Double

    Java double type
    Deal with NaN double value
    Compare two double values
    Java double type creation and comparison
    Java double type conversion

Data Type Conversion

    Java Automatic Type Conversion and Casting
    Data type casting
    Java type promotion
    Autoboxing and auto-unboxing