Skip to content

rationalize fallback result could be improved #61296

@lvlte

Description

@lvlte

When the result of rationalize(T, x, tol) doesn't fit in T (ie. rational components overflows T) the function catches the error and returns the last convergent that has been computed. However, this result is not necessarily the best rational approximation of x for T, as there likely exists a semi-convergent (between the last convergent and the one that throws) that is closer to x. For example :

julia> r1 = Rational{Int128}(pi)
60728338969805745700507212595448411044//19330430665609526556707216376512714945

julia> r2 = 135383245921877291206888365157940675591//43093825600584903152997992180848828034
135383245921877291206888365157940675591//43093825600584903152997992180848828034

julia> abs(BigFloat(r2) - big(pi)) < abs(BigFloat(r1) - big(pi))
true
julia> using Base.MathConstants

julia> r1 = Rational{Int64}(γ)
1055624077073011532//1828820909171081307

julia> r2 = 4434255124552851345//7682146196273606513
4434255124552851345//7682146196273606513

julia> abs(BigFloat(r2) - big(γ)) < abs(BigFloat(r1) - big(γ))
true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions