isnan
isnan(f) -> Bool
Test whether a floating point number is not a number (NaN)
Examples
-
Check if a floating-point number is NaN:
julia> isnan(2.5) false julia> isnan(sqrt(-1)) trueIn these examples, the
isnanfunction is used to check if a given floating-point number is NaN. It returnstrueif the number is NaN andfalseotherwise. -
Handle NaN values in an array:
julia> arr = [1.0, 2.5, NaN, 4.2]; julia> isnan.(arr) 4-element BitArray{1}: false false true falseHere,
isnan.is used in conjunction with broadcasting (.) to check if each element of the arrayarris NaN. It returns a boolean array indicating the presence of NaN values. - Filter out NaN values from an array:
julia> arr = [1.0, 2.5, NaN, 4.2]; julia> filter(!isnan, arr) 3-element Array{Float64,1}: 1.0 2.5 4.2In this example, the
filterfunction is used along with the!isnanpredicate to remove NaN values from the arrayarr. It returns a new array without the NaN elements.
Common mistake example:
julia> isnan("NaN")
ERROR: MethodError: no method matching isnan(::String)
In this example, the isnan function is mistakenly applied to a string instead of a floating-point number. Ensure that the input to isnan is a valid floating-point number to avoid such errors.
See Also
all, all!, angle, any, any!, falses, ifelse, is, isinf, isinteger, isnan, isperm, ispow2, isreal, trues,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.