Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rational powers and coversion #18114

Closed
pwl opened this issue Aug 18, 2016 · 1 comment
Closed

Rational powers and coversion #18114

pwl opened this issue Aug 18, 2016 · 1 comment

Comments

@pwl
Copy link
Contributor

pwl commented Aug 18, 2016

There is something wrong with Rational powers:

julia> BigFloat(2)^(BigFloat(1)/BigFloat(3))-BigFloat(2)^(1//3)
1.615949454734010502584703185359054123712092705937300412204179527347802183365638e-17

where there shouldn't be any difference (1//3 is of infinite precision). The issue comes from the definition of ^ for rationals, where convert is applied to the result of a division of two integers, which is basically a Float64, the result is then converted back to BigFloat

^{T<:AbstractFloat}(x::T, y::Rational) = x^(convert(T, y.num / y.den))
^{T<:AbstractFloat}(x::Complex{T}, y::Rational) = x^(convert(T, y.num / y.den))

Redefining ^ like this

^{T<:AbstractFloat}(x::T, y::Rational) = x^(convert(T, y.num) / convert(T, y.den))
^{T<:AbstractFloat}(x::Complex{T}, y::Rational) = x^(convert(T, y.num) / convert(T, y.den))

fixes the problem:

julia> BigFloat(2)^(BigFloat(1)/BigFloat(3))-BigFloat(2)^(1//3)
0.000000000000000000000000000000000000000000000000000000000000000000000000000000

In summary, convert doesn't commute with division (nor with other similar operations) and convert should be applied as early as possible (directly to the integers in this case) to avoid precision loss. There might be more cases like this that I am not aware of.

EDIT: This bug is new in 0.5 and it was introduced in #17530.

@simonbyrne
Copy link
Contributor

Would you mind opening a pull request?

pwl added a commit to pwl/julia that referenced this issue Aug 18, 2016
pwl added a commit to pwl/julia that referenced this issue Aug 18, 2016
simonbyrne pushed a commit that referenced this issue Aug 19, 2016
Fixes precision problem for rational powers of BigFloats (#18114)
tkelman pushed a commit that referenced this issue Aug 20, 2016
Fixes precision problem for rational powers of BigFloats (#18114)
(cherry picked from commit db03824)
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Fixes precision problem for rational powers of BigFloats (JuliaLang#18114)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants