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

Support two argument at-irrational #46054

Merged
merged 11 commits into from
Apr 22, 2023
30 changes: 20 additions & 10 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,13 @@ end
round(x::Irrational, r::RoundingMode) = round(float(x), r)

"""
@irrational sym val def
@irrational(sym, val, def)
@irrational sym [val] def

Define a new `Irrational` value, `sym`, with pre-computed `Float64` value `val`,
and arbitrary-precision definition in terms of `BigFloat`s given by the expression `def`.
Define a new `Irrational` value, `sym`, with arbitrary-precision definition in terms
of `BigFloat`s given by the expression `def`.

Optionally provide a pre-computed `Float64` value `val` which must equal `Float64(def)`.
`val` will be computed automatically if omitted.

An `AssertionError` is thrown when either `big(def) isa BigFloat` or `Float64(val) == Float64(def)`
returns `false`.
Expand All @@ -184,24 +186,30 @@ returns `false`.

# Examples
```jldoctest
julia> Base.@irrational(twoπ, 6.2831853071795864769, 2*big(π))
julia> Base.@irrational twoπ 2*big(π)

julia> twoπ
twoπ = 6.2831853071795...

julia> Base.@irrational sqrt2 1.4142135623730950488 √big(2)
julia> Base.@irrational sqrt2 1.4142135623730950488 √big(2)

julia> sqrt2
sqrt2 = 1.4142135623730...

julia> Base.@irrational sqrt2 1.4142135623730950488 big(2)
julia> Base.@irrational sqrt2 1.4142135623730950488 big(2)
ERROR: AssertionError: big($(Expr(:escape, :sqrt2))) isa BigFloat

julia> Base.@irrational sqrt2 1.41421356237309 √big(2)
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
Expand All @@ -221,8 +229,10 @@ macro irrational(sym, val, def)
quote
const $esym = Irrational{$qsym}()
$bigconvert
Base.Float64(::Irrational{$qsym}) = $val
Base.Float32(::Irrational{$qsym}) = $(Float32(val))
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))
Expand Down
10 changes: 5 additions & 5 deletions base/mathconstants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ module MathConstants

export π, pi, ℯ, e, γ, eulergamma, catalan, φ, golden

Base.@irrational π 3.14159265358979323846 pi
Base.@irrational ℯ 2.71828182845904523536 exp(big(1))
Base.@irrational γ 0.57721566490153286061 euler
Base.@irrational φ 1.61803398874989484820 (1+sqrt(big(5)))/2
Base.@irrational catalan 0.91596559417721901505 catalan
Base.@irrational π pi
Base.@irrational ℯ exp(big(1))
Base.@irrational γ euler
Base.@irrational φ (1+sqrt(big(5)))/2
Base.@irrational catalan catalan

# aliases
"""
Expand Down
14 changes: 14 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2966,6 +2966,20 @@ end
end
end

Base.@irrational irrational_1548_pi 4863.185427757 1548big(pi)
Base.@irrational irrational_inv_1548_pi 1/big(irrational_1548_pi)
@testset "@irrational" begin
@test irrational_1548_pi ≈ 1548big(pi)
@test Float64(irrational_1548_pi) == 1548π
@test irrational_1548_pi ≈ 1548pi
@test irrational_1548_pi != 1548pi

@test irrational_inv_1548_pi ≈ inv(1548big(pi))
@test Float64(irrational_inv_1548_pi) == 1/(1548π)
@test irrational_inv_1548_pi ≈ inv(1548pi)
@test irrational_inv_1548_pi != inv(1548pi)
end

@testset "modf" begin
@testset "remd" begin
denorm_min = nextfloat(0.0)
Expand Down