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{IntN}(pi) are poor approximations to Pi #16513

Closed
JeffreySarnoff opened this issue May 22, 2016 · 8 comments
Closed

Rational{IntN}(pi) are poor approximations to Pi #16513

JeffreySarnoff opened this issue May 22, 2016 · 8 comments
Labels
domain:rationals The Rational type and values thereof

Comments

@JeffreySarnoff
Copy link
Contributor

These values are better approximations to Pi

Rational{Int32}(pi) = Int32(1068966896) // Int32(340262731)
Rational{Int64}(pi) = Int64(2646693125139304345) // Int64(842468587426513207)
Rational{Int128}(pi) = Int128(60728338969805745700507212595448411044) // Int128(19330430665609526556707216376512714945)

setprecision(BigFloat,500);
Q32pi =  Int32(1068966896) // Int32(340262731);
Q64pi = Int64(2646693125139304345) // Int64(842468587426513207);
Q128pi =  Int128(60728338969805745700507212595448411044) // Int128(19330430665609526556707216376512714945);

Float64(BigFloat(pi)-Q32), Float64(BigFloat(pi)-Rational{Int32}(pi)) # 3.1e-18, 1.2e-16
Float64(BigFloat(pi)-Q64), Float64(BigFloat(pi)-Rational{Int64}(pi)) # 1.4e-38, 1.2e-16
Float64(BigFloat(pi)-Q128) # -6.2e-76

@JeffreySarnoff
Copy link
Contributor Author

And if Rational{64} means Int32//Int32, use Q32pi for Q64pi

@tkelman tkelman added the domain:rationals The Rational type and values thereof label May 22, 2016
@TotalVerb
Copy link
Contributor

TotalVerb commented May 23, 2016

Is this operation meaningful? We have with the revised definitions

julia> 4Rational{Int64}(π)
ERROR: OverflowError()
 in *(::Rational{Int64}, ::Rational{Int64}) at ./rational.jl:196
 in *(::Int64, ::Rational{Int64}) at ./promotion.jl:191
 in eval(::Module, ::Any) at ./boot.jl:226

which is somewhat unfortunate. More precision is not necessarily a good thing.

Rational (from my understanding) is intended for exact arithmetic (approximate arithmetic is done much better using floats). Would it be better to disallow conversion of pi with an InexactError?

@JeffreySarnoff
Copy link
Contributor Author

@TotalVerb How do you get that? Please show me what you changed, how you redefined things.

@TotalVerb
Copy link
Contributor

@JeffreySarnoff What I did to change the definition was:

julia> Base.convert(::Type{Rational{Int64}}, c::Irrational{:π}) = Int64(2646693125139304345) // Int64(842468587426513207)

julia> 4Rational{Int64}(π)
ERROR: OverflowError()
 in *(::Rational{Int64}, ::Rational{Int64}) at ./rational.jl:196
 in *(::Int64, ::Rational{Int64}) at ./promotion.jl:191
 in eval(::Module, ::Any) at ./boot.jl:226

But you can reproduce it without changing anything with

julia> 4 * Int64(2646693125139304345) // Int64(842468587426513207)
ERROR: OverflowError()
 in *(::Rational{Int64}, ::Rational{Int64}) at ./rational.jl:196
 in *(::Int64, ::Rational{Int64}) at ./promotion.jl:191
 in eval(::Module, ::Any) at ./boot.jl:226

@JeffreySarnoff
Copy link
Contributor Author

@TotalVerb Now I understand that.
Pick maximal constant K for which K * Rational{Int64}(pi) should not overflow,
and if K is 2^61, then the current definition is appropriate.
While K should be greater than 4, should it be greater than 2^31?

@JeffreySarnoff
Copy link
Contributor Author

Apparently, not a pressing issue. I am retiring it.

@simonbyrne
Copy link
Contributor

simonbyrne commented May 23, 2016

Good catch, I don't know how that happened!

The problem is this line:

convert{T<:Integer}(::Type{Rational{T}}, x::Irrational) = convert(Rational{T}, Float64(x))

It would be better to call rationalize on a BigFloat, e.g. this gives the correct answer:

rationalize(Int32, big(pi), tol=0)

@JeffreySarnoff
Copy link
Contributor Author

:)

simonbyrne added a commit that referenced this issue Jun 25, 2016
simonbyrne added a commit that referenced this issue Jun 25, 2016
simonbyrne added a commit that referenced this issue Jun 26, 2016
Fixes #16513. Changes generated macro to pure macro, so as to avoid breaking in future compiler changes.
StefanKarpinski pushed a commit that referenced this issue Jun 27, 2016
Fixes #16513. Changes generated macro to pure macro, so as to avoid breaking in future compiler changes.
mfasi pushed a commit to mfasi/julia that referenced this issue Sep 5, 2016
Fixes JuliaLang#16513. Changes generated macro to pure macro, so as to avoid breaking in future compiler changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:rationals The Rational type and values thereof
Projects
None yet
Development

No branches or pull requests

4 participants