When using \pi
or \euler
with the function one
the value is the boolean value: true
rather than 1.0. For other irrational values like sqrt(2) the value is 1.0 as it should be. Why is this?
While \sqrt{2} is not rational, sqrt(2)
is a floating-point number, thus rational.
Why?
Yes, I realized after I typed it that sqrt(2) is not irrational in the language. I did an @code_llvm on the call for pi and it seems it is making a distinction between Floats and irrational numbers. But why is this?
true
in Julia is numerically equal to 1. It is the most “basic” multiplicative identity the sense that multiplying by any other type will up-promote to the precision of the larger type:
julia> true * 1 # returns Int
1
julia> true * 1.0 # returns Float64
1.0
julia> true * 1.0f0 # returns Float32
1.0f0
I think it is returned as the multiplicative identity for Irrational
constants because we’re not sure what other type of 1
to return in that case.
Whereas 1.0
is a Float64
value. This is the multiplicative identity for Float64
types, but you wouldn’t want to use it for arbitrary types because it will cause type promotion to Float64
:
julia> 1.0 * 3 # promotes Int to Float64
3.0
julia> 1.0 * 3.0f0 # promotes Float32 to Float64
3.0
Thank you, that makes sense.