|
| 1 | +package com.baeldung.nan; |
| 2 | + |
| 3 | +/** |
| 4 | + * Sample usage of NaN. |
| 5 | + * |
| 6 | + */ |
| 7 | +public class NaNExample { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + NaNExample naNExample = new NaNExample(); |
| 11 | + naNExample.demo(); |
| 12 | + } |
| 13 | + |
| 14 | + void demo() { |
| 15 | + undefined_operations_produce_NaN(); |
| 16 | + operations_with_no_real_results_produce_NaN(); |
| 17 | + operations_with_NaN_produce_NaN(); |
| 18 | + comparison_with_NaN(); |
| 19 | + check_if_a_value_is_NaN(); |
| 20 | + assign_NaN_to_missing_values(); |
| 21 | + } |
| 22 | + |
| 23 | + void undefined_operations_produce_NaN() { |
| 24 | + System.out.println("Undefined Operations Produce NaN"); |
| 25 | + final double ZERO = 0; |
| 26 | + System.out.println("ZERO / ZERO = " + (ZERO / ZERO)); |
| 27 | + System.out.println("INFINITY - INFINITY = " + (Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY)); |
| 28 | + System.out.println("INFINITY * ZERO = " + (Double.POSITIVE_INFINITY * ZERO)); |
| 29 | + System.out.println(); |
| 30 | + } |
| 31 | + |
| 32 | + void operations_with_no_real_results_produce_NaN() { |
| 33 | + System.out.println("Operations with no real results produce NaN"); |
| 34 | + System.out.println("SQUARE ROOT OF -1 = " + Math.sqrt(-1)); |
| 35 | + System.out.println("LOG OF -1 = " + Math.log(-1)); |
| 36 | + System.out.println(); |
| 37 | + } |
| 38 | + |
| 39 | + void operations_with_NaN_produce_NaN() { |
| 40 | + System.out.println("Operations with NaN produce NaN"); |
| 41 | + System.out.println("2 + NaN = " + (2 + Double.NaN)); |
| 42 | + System.out.println("2 - NaN = " + (2 - Double.NaN)); |
| 43 | + System.out.println("2 * NaN = " + (2 * Double.NaN)); |
| 44 | + System.out.println("2 / NaN = " + (2 / Double.NaN)); |
| 45 | + System.out.println(); |
| 46 | + } |
| 47 | + |
| 48 | + void assign_NaN_to_missing_values() { |
| 49 | + System.out.println("Assign NaN to Missing values"); |
| 50 | + double salaryRequired = Double.NaN; |
| 51 | + System.out.println(salaryRequired); |
| 52 | + System.out.println(); |
| 53 | + } |
| 54 | + |
| 55 | + void comparison_with_NaN() { |
| 56 | + System.out.println("Comparison with NaN"); |
| 57 | + final double NAN = Double.NaN; |
| 58 | + System.out.println("NaN == 1 = " + (NAN == 1)); |
| 59 | + System.out.println("NaN > 1 = " + (NAN > 1)); |
| 60 | + System.out.println("NaN < 1 = " + (NAN < 1)); |
| 61 | + System.out.println("NaN != 1 = " + (NAN != 1)); |
| 62 | + System.out.println("NaN == NaN = " + (NAN == NAN)); |
| 63 | + System.out.println("NaN > NaN = " + (NAN > NAN)); |
| 64 | + System.out.println("NaN < NaN = " + (NAN < NAN)); |
| 65 | + System.out.println("NaN != NaN = " + (NAN != NAN)); |
| 66 | + System.out.println(); |
| 67 | + } |
| 68 | + |
| 69 | + void check_if_a_value_is_NaN() { |
| 70 | + System.out.println("Check if a value is NaN"); |
| 71 | + double x = 1; |
| 72 | + System.out.println(x + " is NaN = " + (x != x)); |
| 73 | + System.out.println(x + " is NaN = " + (Double.isNaN(x))); |
| 74 | + |
| 75 | + x = Double.NaN; |
| 76 | + System.out.println(x + " is NaN = " + (x != x)); |
| 77 | + System.out.println(x + " is NaN = " + (Double.isNaN(x))); |
| 78 | + System.out.println(); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments