Skip to content

Commit 84189cf

Browse files
Ickasersostock
andauthored
Add NaNMath extension for Unitful quantities (#824)
* Add NaNMath support for Unitful quantities * Add compat for NaNMath * Broaden dispatch to AbstractQuantity Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Add a couple tests to make sure that if NaNMath loses fallback for dimensions, will show up here * Test that result has correct units, take u_str macro out of tests Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --------- Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com>
1 parent 82f27da commit 84189cf

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1515
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
1616
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
1717
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
18+
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
1819
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1920

2021
[extensions]
2122
ConstructionBaseUnitfulExt = "ConstructionBase"
2223
ForwardDiffExt = "ForwardDiff"
2324
InverseFunctionsUnitfulExt = "InverseFunctions"
2425
LatexifyExt = ["Latexify", "LaTeXStrings"]
26+
NaNMathExt = "NaNMath"
2527
PrintfExt = "Printf"
2628

2729
[compat]
@@ -33,6 +35,7 @@ InverseFunctions = "0.1"
3335
LaTeXStrings = "1.2.0"
3436
Latexify = "0.16.8"
3537
LinearAlgebra = "<0.0.1, 1"
38+
NaNMath = "1"
3639
Printf = "<0.0.1, 1"
3740
REPL = "<0.0.1, 1"
3841
Random = "<0.0.1, 1"
@@ -47,10 +50,11 @@ InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
4750
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
4851
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
4952
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
53+
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
5054
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
5155
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
5256
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
5357
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
5458

5559
[targets]
56-
test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "Latexify", "LaTeXStrings", "LinearAlgebra", "Test", "Random", "REPL", "Printf"]
60+
test = ["Aqua", "ConstructionBase", "ForwardDiff", "InverseFunctions", "Latexify", "LaTeXStrings", "LinearAlgebra", "NaNMath", "Test", "Random", "REPL", "Printf"]

ext/NaNMathExt.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module NaNMathExt
2+
using Unitful
3+
import NaNMath
4+
5+
NaNMath.sqrt(q::Unitful.AbstractQuantity) = NaNMath.sqrt(ustrip(q))*sqrt(unit(q))
6+
NaNMath.pow(q::Unitful.AbstractQuantity, r) = NaNMath.pow(ustrip(q), r)*unit(q)^r
7+
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Unitful: DimensionError, AffineError
44
import Unitful: LogScaled, LogInfo, Level, Gain, MixedUnits, Decibel
55
import Unitful: FreeUnits, ContextUnits, FixedUnits, AffineUnits, AffineQuantity
66
import ForwardDiff
7+
import NaNMath
78
import Latexify: Latexify, latexify, @latexify, FancyNumberFormatter, SiunitxNumberFormatter
89
import LaTeXStrings: LaTeXString, @L_str
910

@@ -2380,6 +2381,15 @@ if isdefined(Base, :get_extension)
23802381
@test ForwardDiff.Dual(1.0)*u"cm/m" + ForwardDiff.Dual(1.0) == 1.01
23812382
@test ForwardDiff.Dual(1.0)*u"cm/m" == ForwardDiff.Dual(0.01)
23822383
end
2384+
2385+
@testset "NaNMath extension" begin
2386+
@test isnan(NaNMath.sqrt(-1m))
2387+
@test unit(NaNMath.sqrt(-1m)) == m^(1//2)
2388+
@test isnan(NaNMath.pow(-1m, 0.5))
2389+
@test unit(NaNMath.pow(-1m, 0.5)) == m^(1//2)
2390+
@test NaNMath.sqrt(m) === Base.sqrt(m)
2391+
@test NaNMath.pow(m, 2) === m^2
2392+
end
23832393
end
23842394

23852395
struct Num <: Real

0 commit comments

Comments
 (0)