Files
julia/base/mathconstants.jl
Sebastian Stock d3c26b7ecc Extend Base.rationalize instead of defining new function (#56793)
#55886 accidentally created a new function
`Base.MathConstants.rationalize` instead of extending
`Base.rationalize`, which is the reason why `Base.rationalize(Int, π)`
isn’t constant-folded in Julia 1.10 and 1.11:

```
julia> @btime rationalize(Int,π);
  1.837 ns (0 allocations: 0 bytes)      # v1.9: constant-folded
  88.416 μs (412 allocations: 15.00 KiB) # v1.10: not constant-folded
```

This PR fixes that. It should probably be backported to 1.10 and 1.11.
2024-12-19 17:38:45 +08:00

153 lines
3.2 KiB
Julia
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# This file is a part of Julia. License is MIT: https://julialang.org/license
"""
Base.MathConstants
Module containing the mathematical constants.
See [`π`](@ref), [``](@ref), [`γ`](@ref), [`φ`](@ref) and [`catalan`](@ref).
"""
module MathConstants
export π, pi, , e, γ, eulergamma, catalan, φ, golden
Base.@irrational π pi
Base.@irrational exp(big(1))
Base.@irrational γ euler
Base.@irrational φ (1+sqrt(big(5)))/2
Base.@irrational catalan catalan
const _KnownIrrational = Union{
typeof(π), typeof(), typeof(γ), typeof(φ), typeof(catalan)
}
function Rational{BigInt}(::_KnownIrrational)
Base._throw_argument_error_irrational_to_rational_bigint()
end
Base.@assume_effects :foldable function Rational{T}(x::_KnownIrrational) where {T<:Integer}
Base._irrational_to_rational(T, x)
end
Base.@assume_effects :foldable function (::Type{T})(x::_KnownIrrational, r::RoundingMode) where {T<:Union{Float32,Float64}}
Base._irrational_to_float(T, x, r)
end
Base.@assume_effects :foldable function Base.rationalize(::Type{T}, x::_KnownIrrational; tol::Real=0) where {T<:Integer}
Base._rationalize_irrational(T, x, tol)
end
Base.@assume_effects :foldable function Base.lessrational(rx::Rational, x::_KnownIrrational)
Base._lessrational(rx, x)
end
# aliases
"""
π
pi
The constant pi.
Unicode `π` can be typed by writing `\\pi` then pressing tab in the Julia REPL, and in many editors.
See also: [`sinpi`](@ref), [`sincospi`](@ref), [`deg2rad`](@ref).
# Examples
```jldoctest
julia> pi
π = 3.1415926535897...
julia> 1/2pi
0.15915494309189535
```
"""
π, const pi = π
"""
e
The constant .
Unicode `` can be typed by writing `\\euler` and pressing tab in the Julia REPL, and in many editors.
See also: [`exp`](@ref), [`cis`](@ref), [`cispi`](@ref).
# Examples
```jldoctest
julia>
= 2.7182818284590...
julia> log()
1
julia> ^(im)π ≈ -1
true
```
"""
, const e =
"""
γ
eulergamma
Euler's constant.
# Examples
```jldoctest
julia> Base.MathConstants.eulergamma
γ = 0.5772156649015...
julia> dx = 10^-6;
julia> sum(-exp(-x) * log(x) for x in dx:dx:100) * dx
0.5772078382499133
```
"""
γ, const eulergamma = γ
"""
φ
golden
The golden ratio.
# Examples
```jldoctest
julia> Base.MathConstants.golden
φ = 1.6180339887498...
julia> (2ans - 1)^2 ≈ 5
true
```
"""
φ, const golden = φ
"""
catalan
Catalan's constant.
# Examples
```jldoctest
julia> Base.MathConstants.catalan
catalan = 0.9159655941772...
julia> sum(log(x)/(1+x^2) for x in 1:0.01:10^6) * 0.01
0.9159466120554123
```
"""
catalan
# loop over types to prevent ambiguities for ^(::Number, x)
for T in (AbstractIrrational, Rational, Integer, Number, Complex)
Base.:^(::Irrational{:}, x::T) = exp(x)
end
Base.literal_pow(::typeof(^), ::Irrational{:}, ::Val{p}) where {p} = exp(p)
Base.log(::Irrational{:}) = 1 # use 1 to correctly promote expressions like log(x)/log()
Base.log(::Irrational{:}, x::Number) = log(x)
Base.sin(::Irrational{}) = 0.0
Base.cos(::Irrational{}) = -1.0
Base.sincos(::Irrational{}) = (0.0, -1.0)
Base.tan(::Irrational{}) = 0.0
Base.cot(::Irrational{}) = -1/0
end # module