What will be the result of the following code execution?

public class Main {
    public static void main(String[] args) {
        double d = Math.sqrt(-1);
        System.out.println(Double.NaN == d);
        d = d / 0;

When performing the operation, the result of which is not defined mathematically (root of a negative number, 0.0 / 0.0, etc.) returns the special value NaN (Not a Number, not a number). In the wrapper-class of fractional numbers the corresponding constants are declared: Float.NaN and Double.NaN.

A feature of value NaN is that in any arithmetic operations with it it also turns NaN, but when you try to compare it to anything with == it always gets false.

If you want to check whether a value is NaN, then you need to use the methods Float.isNaN (float) or Double.isNaN (double).

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Продовжуйте вивчати
тести з Java
Зареєструйся Зараз
або Підпишись на майбутні тести