I am learning Julia and I would like to create an object in Julia that contains just a single large integer, for example, 1100000. What I could do is write n = 1.1e6
but then the type of this object is Float64
and if I want to use it as an argument for rand()
, I get an error message because the object is not an integer. So instead what I do is as follows.
n = Int64(1.1e6)
rand(n)
But it seems that I am changing the type of the variable here (from Float64
to Int64
) and this should be avoided in Julia as far as I understand. Of course I could use n = 1100000
but this is inefficient and difficult to read in my opinion.
Am I changing the type of the variable here? If yes, is this a good way to change the type of the variable or is there a better way to create an integer using scientific notation without having to change the type of the variable?
Any help is much appreciated!
I would write it as:
n = 1_100_000
for me it is more readable than
n = Int(1.1e6)
(or even 1.1e6
) but of course it is subjective.
Changing type like in Int(1.1e6)
is not a problem in Julia. It will work, as long as passed float represents an integer (otherwise you will get InexactError
error).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With