forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathconstants.jl
126 lines (93 loc) · 2.32 KB
/
mathconstants.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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 π 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
# 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.5772078382499134
```
"""
γ, 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)
end # module