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

Add documentation about failure cases of trig functions & return types #50855

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
inkydragon marked this conversation as resolved.
Show resolved Hide resolved

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.
inkydragon marked this conversation as resolved.
Show resolved Hide resolved

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the 's' accidentally removed? The website works with https for me.

Copy link
Contributor Author

@Seelengrab Seelengrab Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for mentioning this! It did not work for me at the time of writing that commit; since that was more than half a year ago, things might have been fixed since then (an unfortunate sideeffect of PRs lingering). Note that the DOI link below still has its https protocol.

If I'm seeing it correctly, the http link for digizeitschriften.de redirects to the https version.

# [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