Int and float functions

Hello,
Just a question: Why is the Int function capitalized and the float function lower-case?

julia> 0x0001 |> Int
1

julia> 0x0001 |> float
1.0

julia> 0x0001 |> Float
ERROR: UndefVarError: `Float` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[120]:1

julia> 0x0001 |> int
ERROR: UndefVarError: `int` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[121]:1

thanks !
fdekerm

julia> 0x0001 |> Float64
1.0
1 Like

Thanks for the answer, but it doesn’t really answer the question, which is more of a typographical nature.

Int and float both make the assumption of stockage on 64 bytes.

julia> typeof(0x0001 |> Int)
Int64

julia> typeof(0x0001 |> Int64)
Int64

julia> typeof(0x0001 |> float)
Float64

julia> typeof(0x0001 |> Float64)
Float64

Neither is correct. Int is an alias for a type, Int32 on 32-bit systems, Int64 on 64-bit systems. The method of float you invoke forwards to AbstractFloat, whose methods are mostly implemented to forward to Float64, but that’s not a guarantee for all possible methods e.g. AbstractFloat(big(1)). Moreover, the float function has other methods that don’t return an AbstractFloat type, e.g. float(1:5). It’s not a type constructor.

3 Likes

Int is a type name, and the name of the constructor of that type, not just a regular function, and all types are CapitalCase, by convention.

float, on the other hand, is not a type name, and not a constructor.

2 Likes

Int is an alias for the system native integer type. That is usually Int64 these days. For 32-bit systems, it will be Int32.

https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/

Importantly since this refers to a specific type, Int(x) will always return Int.

float is not a direct alias for a specific floating point type such as Float64. It can and does return distinct types depending on the input.

julia> Int(0x8) |> typeof
Int64

julia> float(8.f5) |> typeof
Float32

julia> float(8.5) |> typeof
Float64

julia> float(5 + 7im) |> typeof
ComplexF64 (alias for Complex{Float64})

Int and float are thus not analogous functions. Often if you wanted to convert a non-integer type to an integer you would need to do something such as round(Int, x).

1 Like

I am more curious to know why there is no int while there is a float.

1 Like

int-ifying runs into rounding or magnitude issues far more easily, and there’s no convenient default way to handle those, so might as well manually do it with a bunch of other functions and arguments.