typealias for Float

121 views
Skip to first unread message

James Parker

unread,
Apr 18, 2013, 5:04:17 PM4/18/13
to julia...@googlegroups.com
Is there a typealias for Float?

Currently, I'm using the following:

if WORD_SIZE == 64
    typealias Float Float64
else
    typealias Float Float32
end

This works, but is there a built in definition in Core or somewhere else?

Jameson Nash

unread,
Apr 18, 2013, 5:36:03 PM4/18/13
to julia...@googlegroups.com
I'm not sure what this is supposed to mean. A float in c is always 32 bit and a double is always 64 bit (aside from compiler flags like -fshort-double). The CPU WORD_SIZE has no relevance to the FPU

You can use Cfloat and Cdouble, but those are merely aliases to Float32 and Float64 respectively

James Parker

unread,
Apr 18, 2013, 9:36:31 PM4/18/13
to julia...@googlegroups.com
Aren't int's usually 32 bits and long's 64 bits in C? Yet Julia still has Int aliased to either Int32 or Int64. Shouldn't there be a similar for functionality for Float's?

Jameson Nash

unread,
Apr 18, 2013, 10:06:14 PM4/18/13
to julia...@googlegroups.com
No, that is not correct. Nor would it have any relevance if it was. It's probably best to just use Float64 (this is what C has been defaulting to for decades)

int's and long's are 32 bits on windows and 32-bit linux. long long's are 64 bits everywhere that is currently supported. longs are 64 bit on 64 bit linux. Julia's Int type is the native platform word size, which also corresponds to the sizeof the ptrdiff_t type. This makes it a useful distinction. All of this is completely irrelevant for FloatingPoint types: float's are 32-bit and doubles are 64-bit always (unless you pass special compiler flags which will make your program/library very hard to use). The i387 FPU on your 32-bit computer from over 15 years ago is pretty much just as good at computing with doubles as floats (it's successor, the modern x86_64, is no different). This makes the distinction irrelevant.
(disclaimer: the sizes of c integers given above is  true of the platforms supported by julia, but is not true of all platforms)

The section of the julia manual on calling C code has a table showing these comparisons: http://docs.julialang.org/en/release-0.1/manual/calling-c-and-fortran-code/#type-correspondences

James Parker

unread,
Apr 18, 2013, 10:14:36 PM4/18/13
to julia...@googlegroups.com
Hmm ok. Thanks for clarifying that. I guess I haven't done much windows development.

I still think there should be a built-in 'typealias Float Float64' for convenience and symmetry.

Stefan Karpinski

unread,
Apr 20, 2013, 1:00:09 PM4/20/13
to Julia Users
The reason there's a system-dependent alias for Int to Int32 and Int64 is that 32-bit machines use 32-bit integers and pointers by default, so you want to use Int32 wherever possible, whereas on 64-bit machines, you use 64-bit ints and pointers everywhere. With floating-point arithmetic, there's no such distinction since the 8087 and all subsequent IEEE 754 FPUs have supported both 32-bit and 64-bit floating-point numbers. So just like in C, you specify whether you want 32-bit (float / Float32) or 64-bit (double / Float64) explicitly. If you want to alias Float to Float64 in your code that's totally ok, but it would just be an extra name in base Julia serving no real purpose. It's not really analogous to the Int situation because Float would always be Float64.

James Parker

unread,
Apr 20, 2013, 2:25:34 PM4/20/13
to julia...@googlegroups.com
Ok I'll just use Float64 then. 
Reply all
Reply to author
Forward
0 new messages