From 2e8c2b3723575dc9929065c7de9679909b213b73 Mon Sep 17 00:00:00 2001 From: Andrew Keller Date: Thu, 11 May 2017 09:27:55 -0700 Subject: [PATCH] Close issue #78. --- NEWS.md | 11 +++++++++++ src/Unitful.jl | 13 +++++++------ test/runtests.jl | 30 +++++++++++++----------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/NEWS.md b/NEWS.md index 06ee708c..cf841fe4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,14 @@ +- v0.2.3 + - Dimensionful quantities are no longer accepted for `floor`, `ceil`, `trunc`, `round`, + `isinteger`. The choice of units can yield physically different results. + The functions are defined for dimensionless quantities, and return unitless numbers. + Closes [#78](https://github.com/ajkeller34/Unitful.jl/issues/78). + - Added `gn`, a constant quantity for the gravitational acceleration on earth + [#75](https://github.com/ajkeller34/Unitful.jl/pull/75). + - Added `ge`, the gravitational acceleration on earth as a unit + [#75](https://github.com/ajkeller34/Unitful.jl/pull/75). + - Added `lbf`, pounds-force unit + [#75](https://github.com/ajkeller34/Unitful.jl/pull/75). - v0.2.2 - Fixed a bug in promotion involving `ContextUnits` where the promotion context might not be properly retained. diff --git a/src/Unitful.jl b/src/Unitful.jl index c387bbf4..f6ff95a1 100644 --- a/src/Unitful.jl +++ b/src/Unitful.jl @@ -789,9 +789,6 @@ end abs(x::Quantity) = Quantity(abs(x.val), unit(x)) abs2(x::Quantity) = Quantity(abs2(x.val), unit(x)*unit(x)) -trunc(x::Quantity) = Quantity(trunc(x.val), unit(x)) -round(x::Quantity) = Quantity(round(x.val), unit(x)) - copysign(x::Quantity, y::Number) = Quantity(copysign(x.val,y/unit(y)), unit(x)) flipsign(x::Quantity, y::Number) = Quantity(flipsign(x.val,y/unit(y)), unit(x)) @@ -872,15 +869,19 @@ end ==(x::Number, y::Quantity) = ==(y,x) <=(x::Quantity, y::Quantity) = <(x,y) || x==y -for f in (:zero, :floor, :ceil) - @eval ($f)(x::Quantity) = Quantity(($f)(x.val), unit(x)) +for f in (:floor, :ceil, :trunc, :round, :isinteger) + @eval ($f)(x::Quantity) = error("$($f) can only be well-defined for dimensionless ", + "numbers. For dimensionful numbers, different input units yield physically ", + "different results.") + @eval ($f)(x::DimensionlessQuantity) = ($f)(uconvert(NoUnits, x)) end + +zero(x::Quantity) = Quantity(zero(x.val), unit(x)) zero{T,D,U}(x::Type{Quantity{T,D,U}}) = zero(T)*U() one(x::Quantity) = one(x.val) one{T,D,U}(x::Type{Quantity{T,D,U}}) = one(T) -isinteger(x::Quantity) = isinteger(x.val) isreal(x::Quantity) = isreal(x.val) isfinite(x::Quantity) = isfinite(x.val) isinf(x::Quantity) = isinf(x.val) diff --git a/test/runtests.jl b/test/runtests.jl index caa82030..44e84af9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -520,8 +520,9 @@ end @test inv([1 1; -1 1]u"nm") ≈ [0.5 -0.5; 0.5 0.5]u"nm^-1" end @testset "> Is functions" begin - @test isinteger(1.0m) - @test !isinteger(1.4m) + @test_throws ErrorException isinteger(1.0m) + @test isinteger(1.4m/mm) + @test !isinteger(1.4mm/m) @test isfinite(1.0m) @test !isfinite(Inf*m) @test isnan(NaN*m) @@ -771,14 +772,14 @@ end end @testset "Rounding" begin - @test @inferred(trunc(3.7m)) == 3.0m - @test trunc(-3.7m) == -3.0m - @test @inferred(floor(3.7m)) == 3.0m - @test floor(-3.7m) == -4.0m - @test @inferred(ceil(3.7m)) == 4.0m - @test ceil(-3.7m) == -3.0m - @test @inferred(round(3.7m)) == 4.0m - @test round(-3.7m) == -4.0m + @test_throws ErrorException floor(3.7m) + @test_throws ErrorException ceil(3.7m) + @test_throws ErrorException trunc(3.7m) + @test_throws ErrorException round(3.7m) + @test floor(1.0314m/mm) === 1031.0 + @test ceil(1.0314m/mm) === 1032.0 + @test trunc(-1.0314m/mm) === -1031.0 + @test round(1.0314m/mm) === 1031.0 end @testset "Sgn, abs, &c." begin @@ -949,13 +950,8 @@ end @test typeof(5m .* [1m, 2m, 3m]) == Array{typeof(1u"m^2"),1} @test @inferred(eye(2)*V) == [1.0V 0.0V; 0.0V 1.0V] @test @inferred(V*eye(2)) == [1.0V 0.0V; 0.0V 1.0V] - @static if VERSION >= v"0.6.0-dev.1632" # Make dot ops fusing, PR 17623 - @test_broken @inferred(eye(2).*V) == [1.0V 0.0V; 0.0V 1.0V] - @test_broken @inferred(V.*eye(2)) == [1.0V 0.0V; 0.0V 1.0V] - else - @test @inferred(eye(2).*V) == [1.0V 0.0V; 0.0V 1.0V] - @test @inferred(V.*eye(2)) == [1.0V 0.0V; 0.0V 1.0V] - end + @test @inferred(eye(2).*V) == [1.0V 0.0V; 0.0V 1.0V] + @test @inferred(V.*eye(2)) == [1.0V 0.0V; 0.0V 1.0V] @test @inferred([1V 2V; 0V 3V].*2) == [2V 4V; 0V 6V] @test @inferred([1V, 2V] .* [true, false]) == [1V, 0V] @test @inferred([1.0m, 2.0m] ./ 3) == [1m/3, 2m/3]