Skip to content

Commit

Permalink
cis/cispi without explicit uconvert (calls sincos/sincospi)
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock committed Oct 31, 2024
1 parent 601f9e1 commit ca80deb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base: min, max, floor, ceil, real, imag, conj
import Base: complex, widen, reim # handled in complex.jl
import Base: exp, exp10, exp2, expm1, log, log10, log1p, log2
import Base: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh,
sinpi, cospi, sinc, cosc, cis
sinpi, cospi, sinc, cosc, cis, cispi
import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit
import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan
import Base: copysign, flipsign
Expand Down
6 changes: 5 additions & 1 deletion src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,15 @@ sqrt(x::AbstractQuantity) = Quantity(sqrt(x.val), sqrt(unit(x)))
cbrt(x::AbstractQuantity) = Quantity(cbrt(x.val), cbrt(unit(x)))

for _y in (:sin, :cos, :tan, :asin, :acos, :atan, :sinh, :cosh, :tanh, :asinh, :acosh, :atanh,
:sinpi, :cospi, :tanpi, :sinc, :cosc, :cis, :cispi, :sincospi)
:sinpi, :cospi, :tanpi, :sinc, :cosc, :sincospi)
if isdefined(Base, _y)
@eval Base.$(_y)(x::DimensionlessQuantity) = Base.$(_y)(uconvert(NoUnits, x))
end
end
cis(x::DimensionlessQuantity{<:Real}) = Complex(reverse(sincos(x))...)
cis(x::DimensionlessQuantity{<:Complex}) = exp(-imag(x)) * Complex(reverse(sincos(real(x)))...)
cispi(x::DimensionlessQuantity{<:Real}) = Complex(reverse(sincospi(x))...)
cispi(x::DimensionlessQuantity{<:Complex}) = exp(-*imag(x))) * Complex(reverse(sincospi(real(x)))...)

atan(y::AbstractQuantity{T1,D,U1}, x::AbstractQuantity{T2,D,U2}) where {T1,T2,D,U1,U2} =
atan(promote(y,x)...)
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num
@test @inferred(cbrt(m^3)) === m
@test (2m)^3 === 8*m^3
@test (8m)^(1//3) === 2.0*m^(1//3)
@test @inferred(cis(90°)) im
@test @inferred(cis(90°)) == im
@test @inferred(cis((90 - rad2deg(1)*im)°)) *im

# Test inferrability of literal powers
_pow_m3(x) = x^-3
Expand Down Expand Up @@ -806,6 +807,7 @@ Base.:(<=)(x::Issue399, y::Issue399) = x.num <= y.num
@test @inferred(cosc(1ft/3inch)) === 0.25
if isdefined(Base, :cispi)
@test @inferred(cispi(rad/2)) === complex(0.0, 1.0)
@test @inferred(cispi(rad/2 + im*rad)) complex(0.0, exp(-π))
end
if isdefined(Base, :sincospi)
@test @inferred(sincospi(rad/2)) === (1.0, 0.0)
Expand Down

0 comments on commit ca80deb

Please sign in to comment.