Skip to content

Commit

Permalink
Add unitless dispatch for more trig functions (#387)
Browse files Browse the repository at this point in the history
* Add unitless dispatch for more trig functions

Dispatch on unitless quantities (like `10m/3km`) for inverse and
hyperbolic trig functions. Fixes #332.

* Update Project.toml

Bump to 1.5.0

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>

Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
  • Loading branch information
derikk and giordano committed Oct 20, 2020
1 parent bac587b commit b44c335
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Unitful"
uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.4.1"
version = "1.5.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
4 changes: 2 additions & 2 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import Base: abs, abs2, angle, float, fma, muladd, inv, sqrt, cbrt
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, cot, sec, csc, atan, cis

import Base: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh,
sinpi, cospi, sinc, cosc, cis
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
5 changes: 3 additions & 2 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ end
+(x::AffineQuantity, y::AffineQuantity) = throw(AffineError(
"an invalid operation was attempted with affine quantities: $x + $y"))

# Specialize substraction of affine quantities
# Specialize subtraction of affine quantities
-(x::AffineQuantity, y::AffineQuantity) = -(promote(x,y)...)
function -(x::T, y::T) where T <: AffineQuantity
return Quantity(x.val - y.val, absoluteunit(unit(x)))
Expand Down Expand Up @@ -210,7 +210,8 @@ end
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, :cot, :sec, :csc, :cis)
for _y in (:sin, :cos, :tan, :asin, :acos, :atan, :sinh, :cosh, :tanh, :asinh, :acosh, :atanh,
:sinpi, :cospi, :sinc, :cosc, :cis)
@eval ($_y)(x::DimensionlessQuantity) = ($_y)(uconvert(NoUnits, x))
end

Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,31 @@ end
@test @inferred(csc(90°)) == 1
@test @inferred(sec(0°)) == 1
@test @inferred(cot(45°)) == 1
@test @inferred(asin(1m/1000mm)) == 90°
@test @inferred(acos(-1mm/1000μm)) == π*rad
@test @inferred(atan(2000sqrt(3)ms/2.0s)) == 60°
@test @inferred(acsc(5.0Hz*0.2s)) == π/2
@test @inferred(asec(1m/1nm)) π/2
@test @inferred(acot(2sqrt(3)s/2000ms)) 30°

@test @inferred(sinh(0.0rad)) == 0.0
@test @inferred(sinh(1J/N/m) + cosh(1rad)) MathConstants.e
@test @inferred(tanh(1m/1µm)) == 1
@test @inferred(csch(0.0°)) == Inf
@test @inferred(sech(0K/Ra)) == 1
@test @inferred(coth(1e3m*mm^-1)) == 1
@test @inferred(asinh(0.0mg/kg)) == 0
@test @inferred(acosh(1mm/1000μm)) == 0
@test @inferred(atanh(0W*s/J)) == 0
@test @inferred(acsch(hr/yr * 0)) == Inf
@test @inferred(asech(1.0m/1000.0mm)) == 0
@test @inferred(acoth(1km/1000m)) == Inf

@test @inferred(sinpi(rad/2)) == 1
@test @inferred(cospi(1rad)) == -1
@test @inferred(sinc(1rad)) === 0
@test @inferred(cosc(1ft/3inch)) === 0.25

@test @inferred(atan(m*sqrt(3),1m)) 60°
@test @inferred(atan(m*sqrt(3),1.0m)) 60°
@test @inferred(atan(m*sqrt(3),1000mm)) 60°
Expand Down

2 comments on commit b44c335

@giordano
Copy link
Collaborator

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/23354

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.0 -m "<description of version>" b44c335c466d4ecf007be5bd3aeb8ee4c5debe58
git push origin v1.5.0

Please sign in to comment.