In general you shouldn’t use Complex
, that’s the type, it’s easier to use the constructor complex
. Complex
could be useful only in the case in which you want to force the parameter:
julia> Complex{Float64}(3, -2)
3.0 - 2.0im
In any other case, go with complex
. float
converts the number to the most appropriate “floating-point number”, but there is no Float
type associated. Note: as convention, in Julia type names starts with capital letter, while function names are all lower case.
Well, in Julia there is no Irrational 2pi
However there is tau
from GitHub - JuliaMath/Tau.jl: A Julia module providing the definition of the circle constant Tau (2π) which serves that goal
Yes, that’s exactly the point. Given their nature, irrational numbers can’t be represented numerically by a finite-memory computer, so anyway you end up with a float number with any operations you do. Julia doesn’t do symbolic algebra. Just keep in mind this. The convenience of the Irrational
type in Julia is that they’re automatically converted to the most appropriate precision the first time they get in contact with another number:
julia> pi * 1
3.141592653589793
julia> pi * big(1)
3.141592653589793238462643383279502884197169399375105820974944592307816406286198
You can check the second one has the correct expansion of pi (the very last digit may be wrong), instead the first one is precise up to the 53-bit significand of a Float64
, in fact
julia> big(pi * 1)
3.141592653589793115997963468544185161590576171875
gets soon wrong after the 16-ish significant digit
In that case you should consider to treat irrational numbers in a special way: either do float(x)
(effectively convert to Float64
), or big(x)
in case you want the maximum precision possible or do something else. In case your function has another argument, consider using a promotion rule.