Problem with rounding with big numbers

BigInt(big(2017.)^2017) does not generate the correct value which is generated by: big(2017)^2017. Why is their a rounding error with BigFloat?

You can fix this by calling setprecision with a large enough value:

julia> big(2017.)^2017 == big(2017)^2017
false

julia> setprecision(100000)
100000

julia> big(2017.)^2017 == big(2017)^2017
true

You can also set the precision of individual BigFloats as BigFloat(2017, 100000), but it looks like the precision is not propagated through calculations. In the first case, this method is called:

function ^(x::BigFloat, y::ClongMax)
    z = BigFloat()
    ccall((:mpfr_pow_si, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Clong, Int32), &z, &x, y, ROUNDING_MODE[])
    return z
end

You can see that z is created to store the result of the exponentiation, but it uses the default system precision for BigFloats, not the precision of the BigFloat to be exponentiated.

3 Likes
julia> typeof(big(2017)^2017)
BigInt

julia> typeof(big(2017.)^2017)
BigFloat

BigFloat is rounded (default precision 256 bits), BigInt is exact.