From aa83b3324cdf16101e32cd568e805f9b090e4996 Mon Sep 17 00:00:00 2001 From: chandra Date: Fri, 18 May 2018 16:57:28 -0400 Subject: [PATCH] Nan in Java Example usage of NaN in Java --- .../java/com/baeldung/nan/NaNExample.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/nan/NaNExample.java diff --git a/core-java/src/main/java/com/baeldung/nan/NaNExample.java b/core-java/src/main/java/com/baeldung/nan/NaNExample.java new file mode 100644 index 0000000000..d54a749d3f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/nan/NaNExample.java @@ -0,0 +1,81 @@ +package com.baeldung.nan; + +/** + * Sample usage of NaN. + * + */ +public class NaNExample { + + public static void main(String[] args) { + NaNExample naNExample = new NaNExample(); + naNExample.demo(); + } + + void demo() { + undefined_operations_produce_NaN(); + operations_with_no_real_results_produce_NaN(); + operations_with_NaN_produce_NaN(); + comparison_with_NaN(); + check_if_a_value_is_NaN(); + assign_NaN_to_missing_values(); + } + + void undefined_operations_produce_NaN() { + System.out.println("Undefined Operations Produce NaN"); + final double ZERO = 0; + System.out.println("ZERO / ZERO = " + (ZERO / ZERO)); + System.out.println("INFINITY - INFINITY = " + (Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY)); + System.out.println("INFINITY * ZERO = " + (Double.POSITIVE_INFINITY * ZERO)); + System.out.println(); + } + + void operations_with_no_real_results_produce_NaN() { + System.out.println("Operations with no real results produce NaN"); + System.out.println("SQUARE ROOT OF -1 = " + Math.sqrt(-1)); + System.out.println("LOG OF -1 = " + Math.log(-1)); + System.out.println(); + } + + void operations_with_NaN_produce_NaN() { + System.out.println("Operations with NaN produce NaN"); + System.out.println("2 + NaN = " + (2 + Double.NaN)); + System.out.println("2 - NaN = " + (2 - Double.NaN)); + System.out.println("2 * NaN = " + (2 * Double.NaN)); + System.out.println("2 / NaN = " + (2 / Double.NaN)); + System.out.println(); + } + + void assign_NaN_to_missing_values() { + System.out.println("Assign NaN to Missing values"); + double salaryRequired = Double.NaN; + System.out.println(salaryRequired); + System.out.println(); + } + + void comparison_with_NaN() { + System.out.println("Comparison with NaN"); + final double NAN = Double.NaN; + System.out.println("NaN == 1 = " + (NAN == 1)); + System.out.println("NaN > 1 = " + (NAN > 1)); + System.out.println("NaN < 1 = " + (NAN < 1)); + System.out.println("NaN != 1 = " + (NAN != 1)); + System.out.println("NaN == NaN = " + (NAN == NAN)); + System.out.println("NaN > NaN = " + (NAN > NAN)); + System.out.println("NaN < NaN = " + (NAN < NAN)); + System.out.println("NaN != NaN = " + (NAN != NAN)); + System.out.println(); + } + + void check_if_a_value_is_NaN() { + System.out.println("Check if a value is NaN"); + double x = 1; + System.out.println(x + " is NaN = " + (x != x)); + System.out.println(x + " is NaN = " + (Double.isNaN(x))); + + x = Double.NaN; + System.out.println(x + " is NaN = " + (x != x)); + System.out.println(x + " is NaN = " + (Double.isNaN(x))); + System.out.println(); + } + +}