Skip to content

Commit

Permalink
Merge pull request #176 from dmcclean/fix-175
Browse files Browse the repository at this point in the history
Fix #175 by loosening restrictions on unit construction
  • Loading branch information
dmcclean authored Sep 15, 2016
2 parents adc3bfd + a3bee61 commit e2ae470
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.0.1.3 (2016-09)
-----------------
* Fixed an issue with applying metric prefixes to units with non-rational conversion factors.

1.0.1.2 (2016-05)
-----------------
* Support for GHC 8.0.1-rc4, avoiding GHC Trac issue 12026.
Expand Down
2 changes: 1 addition & 1 deletion dimensional.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dimensional
version: 1.0.1.2
version: 1.0.1.3
license: BSD3
license-file: LICENSE
copyright: Bjorn Buckwalter 2006-2015
Expand Down
25 changes: 15 additions & 10 deletions src/Numeric/Units/Dimensional.hs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,11 @@ we provide a means for converting from type-level dimensions to term-level dimen
-- Supplying negative defining quantities is allowed and handled gracefully, but is discouraged
-- on the grounds that it may be unexpected by other readers.
mkUnitR :: Floating a => UnitName m -> ExactPi -> Unit m1 d a -> Unit m d a
mkUnitR n s' (Unit _ s x) | isExactZero s = error "Supplying zero as a conversion factor is not valid."
| otherwise = Unit n (s' Prelude.* s) (approximateValue s' Prelude.* x)
mkUnitR n s (Unit _ e _) | isExactZero s = error "Supplying zero as a conversion factor is not valid."
| otherwise = Unit n e' x'
where
e' = s Prelude.* e
x' = approximateValue e'

-- | Forms a new atomic 'Unit' by specifying its 'UnitName' and its definition as a multiple of another 'Unit'.
--
Expand All @@ -675,11 +678,12 @@ mkUnitR n s' (Unit _ s x) | isExactZero s = error "Supplying zero as a conversio
--
-- For more information see 'mkUnitR'.
mkUnitQ :: Fractional a => UnitName m -> Rational -> Unit m1 d a -> Unit m d a
mkUnitQ n s' (Unit _ s _) | s' == 0 = error "Supplying zero as a conversion factor is not valid."
| Just q <- toExactRational s'' = Unit n s'' (fromRational q)
| otherwise = error "The resulting conversion factor is not an exact rational."
mkUnitQ n s (Unit _ e x) | s == 0 = error "Supplying zero as a conversion factor is not valid."
| Just x'' <- toExactRational e' = Unit n e' (fromRational x'')
| otherwise = Unit n e' x'
where
s'' = fromRational s' Prelude.* s
e' = fromRational s Prelude.* e
x' = fromRational s Prelude.* x

-- | Forms a new atomic 'Unit' by specifying its 'UnitName' and its definition as a multiple of another 'Unit'.
--
Expand All @@ -688,8 +692,9 @@ mkUnitQ n s' (Unit _ s _) | s' == 0 = error "Supplying zer
--
-- For more information see 'mkUnitR'.
mkUnitZ :: Num a => UnitName m -> Integer -> Unit m1 d a -> Unit m d a
mkUnitZ n s' (Unit _ s _) | s' == 0 = error "Supplying zero as a conversion factor is not valid."
| Just z <- toExactInteger s'' = Unit n s'' (fromInteger z)
| otherwise = error "The resulting conversion factor is not an exact integer."
mkUnitZ n s (Unit _ e x) | s == 0 = error "Supplying zero as a conversion factor is not valid."
| Just x'' <- toExactInteger e' = Unit n e' (fromInteger x'')
| otherwise = Unit n e' x'
where
s'' = fromInteger s' Prelude.* s
e' = fromInteger s Prelude.* e
x' = fromInteger s Prelude.* x

0 comments on commit e2ae470

Please sign in to comment.