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)) true
In these examples, the
isnan
function is used to check if a given floating-point number is NaN. It returnstrue
if the number is NaN andfalse
otherwise. -
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 false
Here,
isnan.
is used in conjunction with broadcasting (.
) to check if each element of the arrayarr
is 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.2
In this example, the
filter
function is used along with the!isnan
predicate 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.