Julia: rand(1e6) returns "ArgumentError"

Created on 21 May 2020  路  1Comment  路  Source: JuliaLang/julia

I can see why this is -- the dimensions of the array we want to create must be an integer, but 1e6 is a float.

This can be a little inconvenient when working with larger numbers however. rand(1000000) is not particularly tidy. Especially if we need to often need to create many large random arrays.

I was thinking maybe we could use multiple dispatch to accept some floats?

Perhaps something a bit like:

function rand(floatValue::Float64)     
    integerValue = convert(Int, floatValue)
    rand(integerValue)
end

This converts the supplied float to an integer and then calls the OG rand on the new int.

It also conveniently stops people from trying, say, rand(2.3)by throwing an InexactError.

Most helpful comment

No we'll not add back using floating points as integers. I don't see how is 1000000 not tidy and there are many other options if you want to make counting easier/typing shorter like 1_000_000 or 10^6 or just do the convertion yourself Int(1e6).

>All comments

No we'll not add back using floating points as integers. I don't see how is 1000000 not tidy and there are many other options if you want to make counting easier/typing shorter like 1_000_000 or 10^6 or just do the convertion yourself Int(1e6).

Was this page helpful?
0 / 5 - 0 ratings