Skip to content

Commit 1dc4b81

Browse files
authored
Merge pull request #17 from ExpandingMan/nounitsdocs
Add clearer docs for explicit NoUnits conversions
2 parents 0193edb + 0b57901 commit 1dc4b81

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ For quantities that are unitless in a given system, the exact unit specified wil
3434
julia> natural(10u"N", unitless, u"s^-1")
3535
1.5326173119543876 s^-1
3636
```
37+
To explicitly check that dimensionless quantities are returned as regular floats, specify `NoUnits`, for
38+
example
39+
```julia
40+
julia> natural(1u"km/s", NoUnits)
41+
3.3356409519815205e-6
42+
43+
julia> natural(1u"kg", NoUnits)
44+
ERROR: DimensionError: and kg c^2 are not dimensionally compatible.
45+
```
3746
The unit that will be used by `natural` can be found using `naturalunit`.
3847
```julia
3948
julia> naturalunit(10u"N")
@@ -76,4 +85,4 @@ Any `NaturalSystem` can be called as a function like `natural`.
7685

7786
julia> unitless(u"kg")
7887
4.5946711112415865e7
79-
```
88+
```

‎src/natural.jl‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ naturalunit(q, units::Units...) = naturalunit(q, DEFAULT_UNITS, units...)
5858
5959
Express `q` in terms of the produts of the given `units`, using natural conversions from `system`.
6060
If no `units` are specified, uses the default units for `system`.
61+
62+
To explicilty check that the the returned values is dimensionless and therefore of a `Real` type, set
63+
`NoUnits`, e.g. `natural(q, sys, NoUnits)`.
6164
"""
6265
function natural(q, system::NaturalSystem, units::Units...)
6366
unit = naturalunit(q, system, units...)
@@ -70,4 +73,4 @@ natural(q, units::Units...) = natural(q, DEFAULT_UNITS, units...)
7073
# For convenience when using custom unit systems
7174
(system::NaturalSystem)(q, units::Units...) = natural(q, system, units...)
7275

73-
const unitless = QG_UNITS
76+
const unitless = QG_UNITS

‎test/runtests.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ using Test
1919
@test (@inferred natural(u"G"^(-1//2), QG_UNITS, u"μg")) ≈ 21.7u"μg" rtol = 0.05
2020

2121
@inferred natural(u"kg^2 / m", PARTICLE_UNITS)
22+
23+
@test_throws Unitful.DimensionError natural(1u"kg", NoUnits)
2224
end

0 commit comments

Comments
 (0)