Dart: Checking For NaN

NaN means “Not a Number.” It happens when a calculation does not give a real number. For example, dividing zero by zero or taking the square root of a negative number can create NaN.

In Dart, NaN is a special value used to show that a number is not valid. It helps your program know when something went wrong in math.

Using double.isNaN Property

You can check if a number is NaN by using the .isNaN property. It returns true when the number is not a valid number, and false otherwise.

void main() {

  double result = 0 / 0; // This produces NaN

  if (result.isNaN) {
    print('The result is Not a Number (NaN)');
  }

}

In this example, we divide zero by zero, which gives NaN. Then, we use .isNaN to check if the result is not a number. Since it is NaN, the program prints a message telling us this.

NaN in Calculations

NaN often appears in math when the result is undefined. For example, dividing zero by zero or taking the square root of a negative number creates NaN.

import 'dart:math';

void main() {

  double value1 = 0 / 0; // NaN
  double value2 = sqrt(-1); // NaN

  if (value1.isNaN) {
    print('0 divided by 0 is NaN');
  }

  if (value2.isNaN) {
    print('Square root of -1 is NaN');
  }

}

In this example, we try two calculations that create NaN. We then check if each result is NaN using .isNaN and print a message if it is. This shows how NaN can appear in real math operations.

Fun Example: NaN Detector Game

Here’s a fun example where we create some random calculations and check if the result is NaN. The program prints out whether each calculation is a number or NaN.

import 'dart:math';

void main() {

  var random = Random();
  List<double> numbers = [0, 1, -1, double.infinity, double.nan];

  for (int i = 0; i < 5; i++) {

    double a = numbers[random.nextInt(numbers.length)];
    double b = numbers[random.nextInt(numbers.length)];

    double result = a / b;

    if (result.isNaN) {
      print('Calculation $a / $b = NaN');
    } else {
      print('Calculation $a / $b = $result');
    }

  }

}

In this example, we pick two random numbers from a list including zero, positive, negative, infinity, and NaN. We divide one by the other and check if the result is NaN. Then we print the result or say it’s NaN. It’s a simple way to explore how NaN can appear!

Comparing NaN with Itself

NaN (Not a Number) is special because it is not equal to itself. This means if you try to compare NaN == NaN, it will return false. To check if a value is NaN, you should use the .isNaN property instead.

void main() {

  double value = double.nan;

  print('Is NaN equal to itself? ${value == double.nan}'); // false

  if (value.isNaN) {
    print('Value is NaN (checked with isNaN)');
  }

}

In this example, comparing value to double.nan returns false because NaN is never equal to anything, even itself. The correct way to check for NaN is by using .isNaN. This prints a message confirming the value is NaN.

Using Conditional Checks to Handle NaN

You can use simple if statements to check if a number is NaN and handle it in your code. For example, if a calculation gives NaN, you might want to replace it with zero or another safe value.

void main() {

  double result = 0 / 0; // This will be NaN

  if (result.isNaN) {
    print('Result is NaN, replacing with 0');
    result = 0;
  }

  print('Final result: $result');

}

In this example, we do a calculation that results in NaN (0 / 0). The if checks if result is NaN using .isNaN. If true, it replaces result with zero. This way, you keep your program safe from unexpected NaN values.

Conclusion

Detecting and handling NaN in Dart is simple. Use the .isNaN property to check if a number is Not a Number. Then, you can decide how to handle it, like replacing NaN with zero. This keeps your code clear and safe. Remember, NaN is special because it does not equal itself, so always use .isNaN to test for it. Knowing this helps you avoid bugs and keep your calculations correct.