What is Double.isNaN in Swift?

Overview

Double.isNaN property in Swift is used to check if an instance of type double is NaN (Not a Number).

Syntax

Double.isNaN

Parameter

This function does not take any parameters.

Return value

The value returned is a boolean value.

Code example

// create some values
let d1 = 1.2
let d2 = 0.0
let d3 = Double.infinity * 0.0
let d4 = Double.infinity
let d5 = Double.nan * 2
// check if Not a Number
print(d1.isNaN) // false
print(d2.isNaN) // false
print(d3.isNaN) // true
print(d4.isNaN) // false
print(d5.isNaN) // true

Explanation

  • Lines 2-6: We create some double values.
  • Lines 9-13: We check to see if the values we created are NaN and print the results to the console.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources