# This file is a part of Julia. License is MIT: https://julialang.org/license ## general machinery for irrational mathematical constants """ AbstractIrrational <: Real Number type representing an exact irrational value, which is automatically rounded to the correct precision in arithmetic operations with other numeric quantities. Subtypes `MyIrrational <: AbstractIrrational` should implement at least `==(::MyIrrational, ::MyIrrational)`, `hash(x::MyIrrational, h::UInt)`, and `convert(::Type{F}, x::MyIrrational) where {F <: Union{BigFloat,Float32,Float64}}`. If a subtype is used to represent values that may occasionally be rational (e.g. a square-root type that represents `√n` for integers `n` will give a rational result when `n` is a perfect square), then it should also implement `isinteger`, `iszero`, `isone`, and `==` with `Real` values (since all of these default to `false` for `AbstractIrrational` types), as well as defining [`hash`](@ref) to equal that of the corresponding `Rational`. """ abstract type AbstractIrrational <: Real end """ Irrational{sym} <: AbstractIrrational Number type representing an exact irrational value denoted by the symbol `sym`, such as [`π`](@ref pi), [`ℯ`](@ref) and [`γ`](@ref Base.MathConstants.eulergamma). See also [`AbstractIrrational`](@ref). """ struct Irrational{sym} <: AbstractIrrational end show(io::IO, x::Irrational{sym}) where {sym} = print(io, sym) function show(io::IO, ::MIME"text/plain", x::Irrational{sym}) where {sym} if get(io, :compact, false)::Bool print(io, sym) else print(io, sym, " = ", string(float(x))[1:min(end,15)], "...") end end promote_rule(::Type{<:AbstractIrrational}, ::Type{Float16}) = Float16 promote_rule(::Type{<:AbstractIrrational}, ::Type{Float32}) = Float32 promote_rule(::Type{<:AbstractIrrational}, ::Type{<:AbstractIrrational}) = Float64 promote_rule(::Type{<:AbstractIrrational}, ::Type{T}) where {T<:Real} = promote_type(Float64, T) promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational,T<:Number} = promote_type(promote_type(S, real(T)), T) AbstractFloat(x::AbstractIrrational) = Float64(x)::Float64 Float16(x::AbstractIrrational) = Float16(Float32(x)::Float32) Complex{T}(x::AbstractIrrational) where {T<:Real} = Complex{T}(T(x)) # XXX this may change `DEFAULT_PRECISION`, thus not effect free @assume_effects :total function Rational{T}(x::AbstractIrrational) where T<:Integer o = precision(BigFloat) p = 256 while true setprecision(BigFloat, p) bx = BigFloat(x) r = rationalize(T, bx, tol=0) if abs(BigFloat(r) - bx) > eps(bx) setprecision(BigFloat, o) return r end p += 32 end end Rational{BigInt}(x::AbstractIrrational) = throw(ArgumentError("Cannot convert an AbstractIrrational to a Rational{BigInt}: use rationalize(BigInt, x) instead")) @assume_effects :total function (t::Type{T})(x::AbstractIrrational, r::RoundingMode) where T<:Union{Float32,Float64} setprecision(BigFloat, 256) do T(BigFloat(x)::BigFloat, r) end end float(::Type{<:AbstractIrrational}) = Float64 ==(::Irrational{s}, ::Irrational{s}) where {s} = true ==(::AbstractIrrational, ::AbstractIrrational) = false <(::Irrational{s}, ::Irrational{s}) where {s} = false function <(x::AbstractIrrational, y::AbstractIrrational) Float64(x) != Float64(y) || throw(MethodError(<, (x, y))) return Float64(x) < Float64(y) end <=(::Irrational{s}, ::Irrational{s}) where {s} = true <=(x::AbstractIrrational, y::AbstractIrrational) = x==y || x Base.@irrational twoπ 2*big(π) julia> twoπ twoπ = 6.2831853071795... julia> Base.@irrational sqrt2 1.4142135623730950488 √big(2) julia> sqrt2 sqrt2 = 1.4142135623730... julia> Base.@irrational sqrt2 1.4142135623730950488 big(2) ERROR: AssertionError: big($(Expr(:escape, :sqrt2))) isa BigFloat julia> Base.@irrational sqrt2 1.41421356237309 √big(2) ERROR: AssertionError: Float64($(Expr(:escape, :sqrt2))) == Float64(big($(Expr(:escape, :sqrt2)))) ``` """ macro irrational(sym, val, def) irrational(sym, val, def) end macro irrational(sym, def) irrational(sym, :(big($(esc(sym)))), def) end function irrational(sym, val, def) esym = esc(sym) qsym = esc(Expr(:quote, sym)) bigconvert = isa(def,Symbol) ? quote function Base.BigFloat(::Irrational{$qsym}, r::MPFR.MPFRRoundingMode=MPFR.ROUNDING_MODE[]; precision=precision(BigFloat)) c = BigFloat(;precision=precision) ccall(($(string("mpfr_const_", def)), :libmpfr), Cint, (Ref{BigFloat}, MPFR.MPFRRoundingMode), c, r) return c end end : quote function Base.BigFloat(::Irrational{$qsym}; precision=precision(BigFloat)) setprecision(BigFloat, precision) do $(esc(def)) end end end quote const $esym = Irrational{$qsym}() $bigconvert let v = $val, v64 = Float64(v), v32 = Float32(v) Base.Float64(::Irrational{$qsym}) = v64 Base.Float32(::Irrational{$qsym}) = v32 end @assert isa(big($esym), BigFloat) @assert Float64($esym) == Float64(big($esym)) @assert Float32($esym) == Float32(big($esym)) end end big(x::AbstractIrrational) = BigFloat(x) big(::Type{<:AbstractIrrational}) = BigFloat # align along = for nice Array printing function alignment(io::IO, x::AbstractIrrational) m = match(r"^(.*?)(=.*)$", sprint(show, x, context=io, sizehint=0)) m === nothing ? (length(sprint(show, x, context=io, sizehint=0)), 0) : (length(something(m.captures[1])), length(something(m.captures[2]))) end # inv inv(x::AbstractIrrational) = 1/x