Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for numeric value in Julia

Tags:

types

julia

I want to determine if a value is numeric or not before trying to use a function on it. As a specific example:

z = [1.23,"foo"]
for val in z
    if isnumeric(val)
        round(z)
    end
end

Here isnumeric() is a function that I don't think exists in Julia. I can think of a few different ways this might be done, but I would like to see some suggestions for the "best" way.

like image 898
Brett Avatar asked Sep 08 '16 17:09

Brett


2 Answers

I think the preferred idiom is

isa(val, Number)

Normally you are interested in rounding floats, in which case

isa(val, AbstractFloat)
like image 143
DNF Avatar answered Oct 08 '22 18:10

DNF


You can check the element's type like this:

typeof(val)<:Number

The :< operator checks if a type is a subtype of another.

Here is a very helpful chart giving an overview of numeric types in Julia: https://en.wikibooks.org/wiki/Introducing_Julia/Types

like image 21
Michael Ohlrogge Avatar answered Oct 08 '22 17:10

Michael Ohlrogge