Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is numeric Julia

Been scouring the internet trying to figure this out. Tried isnumeric, but that only works for an AbstractChar. I'd prefer not having to use tryparse if possible, but if that's the only solution so be it... If it is, why hasn't a function for checking if a string is numeric been implemented yet?

like image 436
Masterfoxify Avatar asked Jun 12 '19 08:06

Masterfoxify


2 Answers

The fastest solution I've found is using tryparse as recommended.

function check_str2(a)
    return tryparse(Float64, a) !== nothing
end

It is on average 20ns compared to 40 for the regex.

The main reason there is no way to check if a string is valid as an int without converting is that there aren't really many compelling use cases for doing so in places where performance matters. In most places, you want to know if something can be parsed as a number to use it as a number, and in the rare off case, the extra couple ns probably doesn't matter.

like image 141
Oscar Smith Avatar answered Nov 08 '22 21:11

Oscar Smith


You normally use a regular expression to check if a string is a number:

julia> re = r"^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$";

julia> occursin(re,"123.")
true

julia> occursin(re,"123.0")
true

julia> occursin(re,"123.012")
true

julia> occursin(re,"123")
true 

julia> occursin(re,"ab")
false

julia> occursin(re,"ab123.1")
false

julia> occursin(re,"123.1e")
false

Note: I have used the regular expression found at Regular expression for floating point numbers If you just want to have an integer part or include the exponent, such ready regular expressions are also easy to find.

EDIT: Benchmark test.

Let us consider the following function to check whether a String is a number:

function check_str(a)
    try
        parse(Float64,a)
        true
    catch
        false
    end
end

Here are the benchmark tests. Note that the regular expression is roughly 200x faster (the increase would be smaller if we decided to look also for the exponent part) and does not allocate.

julia> using BenchmarkTools

julia> @btime check_str("60.0a")
  15.359 μs (18 allocations: 816 bytes)
false

julia> @btime occursin($re,"60.0a")
  67.023 ns (0 allocations: 0 bytes)
false

When the String is successfully parsed the speed gap is much smaller:

julia> @btime check_str("60.0")
  298.833 ns (0 allocations: 0 bytes)
true

julia> @btime occursin($re,"60.0")
  58.865 ns (0 allocations: 0 bytes)
true
like image 5
Przemyslaw Szufel Avatar answered Nov 08 '22 22:11

Przemyslaw Szufel