Skip to content

Commit

Permalink
Add documentation about failure cases of trig functions & return types (
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab authored and tecosaur committed Mar 4, 2024
1 parent f7c27e8 commit ca586f8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 42 deletions.
59 changes: 41 additions & 18 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ log(b::T, x::T) where {T<:Number} = log(x)/log(b)
"""
log(b,x)
Compute the base `b` logarithm of `x`. Throws [`DomainError`](@ref) for negative
Compute the base `b` logarithm of `x`. Throw a [`DomainError`](@ref) for negative
[`Real`](@ref) arguments.
# Examples
Expand Down Expand Up @@ -408,13 +408,17 @@ const libm = Base.libm_name
sinh(x)
Compute hyperbolic sine of `x`.
See also [`sin`](@ref).
"""
sinh(x::Number)

"""
cosh(x)
Compute hyperbolic cosine of `x`.
See also [`cos`](@ref).
"""
cosh(x::Number)

Expand Down Expand Up @@ -493,10 +497,12 @@ asinh(x::Number)

# functions that return NaN on non-NaN argument for domain error
"""
sin(x)
sin(x::T) where {T <: Number} -> float(T)
Compute sine of `x`, where `x` is in radians.
Throw a [`DomainError`](@ref) if `isinf(x)`, return a `T(NaN)` if `isnan(x)`.
See also [`sind`](@ref), [`sinpi`](@ref), [`sincos`](@ref), [`cis`](@ref), [`asin`](@ref).
# Examples
Expand Down Expand Up @@ -524,26 +530,34 @@ julia> round(exp(im*pi/6), digits=3)
sin(x::Number)

"""
cos(x)
cos(x::T) where {T <: Number} -> float(T)
Compute cosine of `x`, where `x` is in radians.
Throw a [`DomainError`](@ref) if `isinf(x)`, return a `T(NaN)` if `isnan(x)`.
See also [`cosd`](@ref), [`cospi`](@ref), [`sincos`](@ref), [`cis`](@ref).
"""
cos(x::Number)

"""
tan(x)
tan(x::T) where {T <: Number} -> float(T)
Compute tangent of `x`, where `x` is in radians.
Throw a [`DomainError`](@ref) if `isinf(x)`, return a `T(NaN)` if `isnan(x)`.
See also [`tanh`](@ref).
"""
tan(x::Number)

"""
asin(x)
asin(x::T) where {T <: Number} -> float(T)
Compute the inverse sine of `x`, where the output is in radians.
Return a `T(NaN)` if `isnan(x)`.
See also [`asind`](@ref) for output in degrees.
# Examples
Expand All @@ -558,9 +572,11 @@ julia> asind.((0, 1/2, 1))
asin(x::Number)

"""
acos(x)
acos(x::T) where {T <: Number} -> float(T)
Compute the inverse cosine of `x`, where the output is in radians
Return a `T(NaN)` if `isnan(x)`.
"""
acos(x::Number)

Expand All @@ -583,9 +599,12 @@ atanh(x::Number)
Compute the natural logarithm of `x`.
Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
Use complex arguments to obtain complex results.
Has a branch cut along the negative real axis, for which `-0.0im` is taken to be below the axis.
Throw a [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
Use [`Complex`](@ref) arguments to obtain [`Complex`](@ref) results.
!!! note "Branch cut"
`log` has a branch cut along the negative real axis; `-0.0im` is taken
to be below the axis.
See also [`ℯ`](@ref), [`log1p`](@ref), [`log2`](@ref), [`log10`](@ref).
Expand Down Expand Up @@ -619,7 +638,7 @@ log(x::Number)
"""
log2(x)
Compute the logarithm of `x` to base 2. Throws [`DomainError`](@ref) for negative
Compute the logarithm of `x` to base 2. Throw a [`DomainError`](@ref) for negative
[`Real`](@ref) arguments.
See also: [`exp2`](@ref), [`ldexp`](@ref), [`ispow2`](@ref).
Expand Down Expand Up @@ -652,7 +671,7 @@ log2(x)
log10(x)
Compute the logarithm of `x` to base 10.
Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
Throw a [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
# Examples
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
Expand All @@ -675,7 +694,7 @@ log10(x)
"""
log1p(x)
Accurate natural logarithm of `1+x`. Throws [`DomainError`](@ref) for [`Real`](@ref)
Accurate natural logarithm of `1+x`. Throw a [`DomainError`](@ref) for [`Real`](@ref)
arguments less than -1.
# Examples
Expand Down Expand Up @@ -706,12 +725,15 @@ end
Return ``\\sqrt{x}``.
Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
Use complex negative arguments instead. Note that `sqrt` has a branch cut
along the negative real axis.
Throw a [`DomainError`](@ref) for negative [`Real`](@ref) arguments.
Use [`Complex`](@ref) negative arguments instead to obtain a [`Complex`](@ref) result.
The prefix operator `√` is equivalent to `sqrt`.
!!! note "Branch cut"
`sqrt` has a branch cut along the negative real axis; `-0.0im` is taken
to be below the axis.
See also: [`hypot`](@ref).
# Examples
Expand Down Expand Up @@ -1005,7 +1027,8 @@ ldexp(x::Float16, q::Integer) = Float16(ldexp(Float32(x), q))
"""
exponent(x::Real) -> Int
Returns the largest integer `y` such that `2^y ≤ abs(x)`.
Return the largest integer `y` such that `2^y ≤ abs(x)`.
For a normalized floating-point number `x`, this corresponds to the exponent of `x`.
Throws a `DomainError` when `x` is zero, infinite, or [`NaN`](@ref).
For any other non-subnormal floating-point number `x`, this corresponds to the exponent bits of `x`.
Expand Down Expand Up @@ -1330,8 +1353,8 @@ end

function add22condh(xh::Float64, xl::Float64, yh::Float64, yl::Float64)
# This algorithm, due to Dekker, computes the sum of two
# double-double numbers and returns the high double. References:
# [1] https://www.digizeitschriften.de/en/dms/img/?PID=GDZPPN001170007
# double-double numbers and return the high double. References:
# [1] http://www.digizeitschriften.de/en/dms/img/?PID=GDZPPN001170007
# [2] https://doi.org/10.1007/BF01397083
r = xh+yh
s = (abs(xh) > abs(yh)) ? (xh-r+yh+yl+xl) : (yh-r+xh+xl+yl)
Expand Down

0 comments on commit ca586f8

Please sign in to comment.