Skip to content

Commit

Permalink
Assorted cleanup (#10)
Browse files Browse the repository at this point in the history
* correct alignment in angle units definitions

* mention turn-based values for all angle unit definitions

* documentation improvements

* use consistent variable names

* wrap a long line to improve legibility

* consistently use spaces after commas
  • Loading branch information
waldyrious authored and yakir12 committed Dec 9, 2017
1 parent b5a8e73 commit c3dc5cd
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/UnitfulAngles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export @u_str
import Base: sin, cos, tan, sec, csc, cot, asin, acos, atan, asec, acsc, acot, atan2, convert

######################### Angle units ##########################################
@unit turn "τ" Turn 2π*u"rad" false
@unit turn "τ" Turn 2π*u"rad" false
@unit halfTurn "π" HalfTurn turn//2 false
@unit quadrant "" Quadrant turn//4 false
@unit sextant "sextant" Sextant turn//6 false
Expand All @@ -19,32 +19,33 @@ import Base: sin, cos, tan, sec, csc, cot, asin, acos, atan, asec, acsc, acot, a
@unit compassPoint "compassPoint" CompassPoint turn//32 false
@unit hexacontade "hexacontade" Hexacontade turn//60 false
@unit brad "brad" BinaryRadian turn//256 false
@unit diameterPart "diameterPart" DiameterPart 1u"rad"/60 false
@unit diameterPart "diameterPart" DiameterPart 1u"rad"/60 false # ≈ turn/377
@unit grad "" Gradian turn//400 false
@unit arcminute "" Arcminute u"°"//60 false
@unit arcsecond "" Arcsecond u"°"//3600 false
@unit arcminute "" Arcminute u"°"//60 false # = turn/21,600
@unit arcsecond "" Arcsecond u"°"//3600 false # = turn/1,296,000

######################### Functions ############################################

# cos and sin have *pi versions, and *d versions
for _f in (:cos, :sin)
@eval $_f{T}(x::Quantity{T,typeof(NoDims),typeof(halfTurn)}) = $(Symbol("$(_f)pi"))(ustrip(x))
@eval $_f{T}(x::Quantity{T,typeof(NoDims),typeof(diameterPart)}) = $_f(ustrip(uconvert(u"rad", x)))
for _x in (turn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval $_f{T}(x::Quantity{T,typeof(NoDims),typeof($_x)}) = $(Symbol("$(_f)pi"))(ustrip(uconvert(halfTurn, x)))
@eval $_f{T}(x::Quantity{T, typeof(NoDims), typeof(halfTurn)}) = $(Symbol("$(_f)pi"))(ustrip(x))
@eval $_f{T}(x::Quantity{T, typeof(NoDims), typeof(diameterPart)}) = $_f(ustrip(uconvert(u"rad", x)))
for _u in (turn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval $_f{T}(x::Quantity{T, typeof(NoDims), typeof($_u)}) = $(Symbol("$(_f)pi"))(ustrip(uconvert(halfTurn, x)))
end
end

# These functions don't have *pi versions, but have *d versions
for _f in (:tan, :sec, :csc, :cot)
@eval $_f{T}(x::Quantity{T,typeof(NoDims),typeof(diameterPart)}) = $_f(ustrip(uconvert(u"rad", x)))
for _x in (turn, halfTurn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval $_f{T}(x::Quantity{T,typeof(NoDims),typeof($_x)}) = $(Symbol("$(_f)d"))(ustrip(uconvert(u"°", x)))
@eval $_f{T}(x::Quantity{T, typeof(NoDims), typeof(diameterPart)}) = $_f(ustrip(uconvert(u"rad", x)))
for _u in (turn, halfTurn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval $_f{T}(x::Quantity{T, typeof(NoDims), typeof($_u)}) = $(Symbol("$(_f)d"))(ustrip(uconvert(u"°", x)))
end
end

# Inverse functions
for _f in (:acos, :asin, :atan, :asec, :acsc, :acot), _u in (diameterPart, u"°", u"rad", turn, halfTurn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
for _f in (:acos, :asin, :atan, :asec, :acsc, :acot),
_u in (diameterPart, u"°", u"rad", turn, halfTurn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval $_f(::typeof($_u), x::Number) = uconvert($_u, $_f(x)*u"rad")
end

Expand All @@ -53,20 +54,20 @@ for _u in (diameterPart, u"°", u"rad", turn, halfTurn, quadrant, sextant, octan
end

# Fun conversion between time and angles
# NOTE: not sure if to use `convert` or `uconvert`
# NOTE: not sure whether to use `convert` or `uconvert`
for _u in (diameterPart, u"°", u"rad", turn, halfTurn, quadrant, sextant, octant, clockPosition, hourAngle, compassPoint, hexacontade, brad, grad, arcminute, arcsecond)
@eval begin
function convert(::typeof($_u), t::Dates.Time)
x = t - Dates.Time(0,0,0)
x = t - Dates.Time(0, 0, 0)
S = typeof(x)
uconvert($_u, x/convert(S, Dates.Hour(1))*hourAngle)
end
end
@eval convert{T}(::Type{Dates.Time}, x::Quantity{T,typeof(NoDims),typeof($_u)}) = Dates.Time(0,0,0) + Dates.Nanosecond(round(Int, ustrip(uconvert(hourAngle, x))*3600000000000))
@eval convert{T}(::Type{Dates.Time}, x::Quantity{T, typeof(NoDims), typeof($_u)}) = Dates.Time(0, 0, 0) + Dates.Nanosecond(round(Int, ustrip(uconvert(hourAngle, x))*3600000000000))
end


# As per the Unitful documentation
# Enable precompilation with Unitful extended units
# http://ajkeller34.github.io/Unitful.jl/stable/extending/#precompilation
const localunits = Unitful.basefactors
function __init__()
merge!(Unitful.basefactors, localunits)
Expand Down

0 comments on commit c3dc5cd

Please sign in to comment.