Int and Float

148 views
Skip to first unread message

Christoph Ortner

unread,
Feb 9, 2015, 2:34:51 PM2/9/15
to julia...@googlegroups.com

Julia, conveniently provides an `Int` type, which is alias for `Int64`. I like this because it frees me from thinking about which integer type to use.  But there is no corresponding `Float` type which is alias for `Float64`. Why?

It seems a trivial thing, but who knows what shifts will happen in floating point computation over the next 10 years? Another reason why it could be nice to have this, is because it would then let me set Float=Float32 locally or globally if  wanted to experiment with it. I realise I can do that anyways, but it would be nicer to have a standard name?

Thanks,
    Christoph

Jiahao Chen

unread,
Feb 9, 2015, 2:38:38 PM2/9/15
to julia...@googlegroups.com
Int is useful for referring to the system’s native signed integer type, which is Int32 on 32-bit systems and Int64 on 64-bit systems. My understanding is that on modern CPU architectures, both Float32 and Float64 are equally "native".

Thanks,

Jiahao Chen
Staff Research Scientist
MIT Computer Science and Artificial Intelligence Laboratory

Christoph Ortner

unread,
Feb 9, 2015, 4:57:09 PM2/9/15
to julia...@googlegroups.com
Thanks for clarifying; so in fact I am using `Int` the wrong way, and should normally specify whether I want Int16, Int32, Int64?

Thank you,
    Christoph

Jiahao Chen

unread,
Feb 9, 2015, 5:22:52 PM2/9/15
to julia...@googlegroups.com
The code

x = 5

will produce a variable x of type Int.

Generally it will be safe to write code like

foo(maxiter::Int = 1000) = for i=1:maxiter do_stuff() end

It would be somewhat overkill to define foo(maxiter::Integer) as Julia will generate not only the method foo(::Int), but also foo(::Int8), foo(::BigInt) and so on for each subtype of Integer. In this particular example, one would not expect any of these other methods to ever be used.

If your code needs to assume something about the size of the integer, go with Int32 or Int64 instead.

Thanks,

Jiahao Chen
Staff Research Scientist
MIT Computer Science and Artificial Intelligence Laboratory

Christoph Ortner

unread,
Feb 9, 2015, 5:27:15 PM2/9/15
to julia...@googlegroups.com
ok - thanks a lot.
   Christoph

Stefan Karpinski

unread,
Feb 9, 2015, 5:38:26 PM2/9/15
to Julia Users
In particular, there's a lot of code where it's reasonable to limit the size of the integers you're working with to the platform native word size – if you're on a 32-bit system, you're not going to have arrays longer than 2^32, nor are you going to have more different values in memory than 2^32.

Páll Haraldsson

unread,
Feb 10, 2015, 5:56:14 AM2/10/15
to julia...@googlegroups.com

On Monday, February 9, 2015 at 10:22:52 PM UTC, Jiahao Chen wrote:

It would be somewhat overkill to define foo(maxiter::Integer) as Julia will generate not only the method foo(::Int), but also foo(::Int8), foo(::BigInt) and so on for each subtype of Integer.

Is this for sure true? I mean, my understanding was that code is only generated when a function is first called, and if say only with the parameter Int32 then there would be no difference? If you want to exclude the possibility of using BigInt then your way seems better, but why would you (say for other than array length)? If generated for every possible sub-type it would seems to be a problem for the type Any..

-- 
Palli.
 
Reply all
Reply to author
Forward
0 new messages