Skip to content

Commit 4a681b7

Browse files
simonbyrnetkelman
authored andcommitted
Add accuracy note for 2-arg log (#18360)
* Add accuracy note for 2-arg log Addresses #18298. * update based on comments (cherry picked from commit 5f74d16)
1 parent 8af23c3 commit 4a681b7

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,13 +2571,6 @@ faster and more accurate.
25712571
"""
25722572
log(x)
25732573

2574-
"""
2575-
log(b,x)
2576-
2577-
Compute the base `b` logarithm of `x`. Throws `DomainError` for negative `Real` arguments.
2578-
"""
2579-
log(b, x)
2580-
25812574
"""
25822575
trunc([T,] x, [digits, [base]])
25832576

base/math.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ deg2rad(z::Real) = deg2rad(float(z))
120120
@vectorize_1arg Real deg2rad
121121

122122
log{T<:Number}(b::T, x::T) = log(x)/log(b)
123+
124+
"""
125+
log(b,x)
126+
127+
Compute the base `b` logarithm of `x`. Throws `DomainError` for negative `Real` arguments.
128+
129+
```jldoctest
130+
julia> log(4,8)
131+
1.5
132+
133+
julia> log(4,2)
134+
0.5
135+
```
136+
137+
!!! note
138+
If `b` is a power of 2 or 10, `log2` or `log10` should be used, as these will
139+
typically be faster and more accurate. For example,
140+
141+
```jldoctest
142+
julia> log(100,1000000)
143+
2.9999999999999996
144+
145+
julia> log10(1000000)/2
146+
3.0
147+
```
148+
"""
123149
log(b::Number, x::Number) = log(promote(b,x)...)
124150
@vectorize_2arg Number log
125151

doc/stdlib/math.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,26 @@ Mathematical Functions
794794
795795
Compute the base ``b`` logarithm of ``x``\ . Throws ``DomainError`` for negative ``Real`` arguments.
796796

797+
.. doctest::
798+
799+
julia> log(4,8)
800+
1.5
801+
802+
julia> log(4,2)
803+
0.5
804+
805+
.. note::
806+
If ``b`` is a power of 2 or 10, ``log2`` or ``log10`` should be used, as these will typically be faster and more accurate. For example,
807+
808+
.. doctest::
809+
810+
julia> log(100,1000000)
811+
2.9999999999999996
812+
813+
julia> log10(1000000)/2
814+
3.0
815+
816+
797817
.. function:: log2(x)
798818

799819
.. Docstring generated from Julia source

0 commit comments

Comments
 (0)