@@ -2,7 +2,7 @@ module Test.Main where
3import Prelude
3import Prelude
5−import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString, P100)
5+import Data.Fixed (class KnownPrecision, Fixed, PProxy, fromNumber, toNumber, fromString, reifyPrecision, reflectPrecision, toString, P100, rescale, fromBigInt, floor)
6import Data.BigInt as BigInt
6import Data.BigInt as BigInt
7import Data.Maybe (Maybe(..), fromJust, isJust)
7import Data.Maybe (Maybe(..), fromJust, isJust)
8import Effect (Effect)
8import Effect (Effect)
@@ -27,6 +27,28 @@ fromNumberWith
27 -> Maybe (Fixed precision)
27 -> Maybe (Fixed precision)
28fromNumberWith _ = fromNumber
28fromNumberWith _ = fromNumber
30+-- | For type hinting.
36+withPrecisionOf _ x = x
40+ . KnownPrecision precision
47+ <<< (_ * scaleFactor)
50+ fromBigInt (BigInt.pow (BigInt.fromInt 10) (BigInt.fromInt p))
30main :: Effect Unit
52main :: Effect Unit
32 log "Commutative monoid under addition:"
54 log "Commutative monoid under addition:"
@@ -123,3 +145,31 @@ main = do
123 Nothing ->
145 Nothing ->
125 <?> ("failed to roundtrip via Number: " <> show { precision, x })
147 <?> ("failed to roundtrip via Number: " <> show { precision, x })
153+ log "rescaling up then down should be no-op"
156+ hi <- chooseInt 10 20
157+ unsafePartial fromJust $ join $
158+ reifyPrecision lo \lo_ ->
159+ reifyPrecision hi \hi_ -> do
161+ let y = withPrecisionOf hi_ $ rescale x
163+ pure $ x == xx <?> show { x, xx, y }
165+ log "rescaling down then up should be the same as flooring"
168+ lo <- chooseInt 10 20
169+ unsafePartial fromJust $ join $
170+ reifyPrecision lo \lo_ ->
171+ reifyPrecision hi \hi_ -> do
173+ let y = withPrecisionOf lo_ $ rescale x
174+ let xx = floorToPrecision lo x
175+ pure $ xx == rescale y <?> show { x, xx, y }