Odd result with negative exponents of 10 in Julia 1.5.2

Hi, I’m relatively new to Julia and just installed version 1.5.2. Can someone explain to me how exponentiation works in this language? This might be incredibly basic, but negative exponents of 10 that are less than -1 throw incorrect answers.

Normal results:
julia> 10^-1
0.1
julia> 10^2
100
julia> 2^-4
0.0625
julia> 1e-5
1.0e-5

Weird results:
julia> 10^-2
0.010000000000000002
julia> 10^-5
1.0000000000000003e-5

Thanks in advance!

This is just that Julia (and most programming languages) use Float64 as their default numeric type. Fundamentally computers can’t store all real numbers, so no approach will never round for all numbers.

3 Likes

Thanks!

yeah when you type 0.01, the 0.01 it shows is an illusion:

julia> 0.01
0.01

julia> using Printf

julia> @sprintf "%.19f" 0.01
"0.0100000000000000002"
2 Likes

For lots more detail, see: PSA: floating-point arithmetic

2 Likes