@@ -1,398 +0,0 @@
3−This module defines a numeric type `Fixed` for working with
4−fixed point numbers in base 10. The precision is tracked in
10−newtype Fixed (precision :: Precision)
13−A fixed point representation of a real number, with the specified precision.
15−A value is multiplied by the precision, truncated and stored as a big
16−integer. That is, we approximate the number by numerator/10^precision, storing
17−only the numerator, and carrying the precision around as type information.
19−The `Semiring` and associated instances allow us to perform basic arithmetic
24−(KnownPrecision precision) => Show (Fixed precision)
27−(KnownPrecision precision) => Semiring (Fixed precision)
28−(KnownPrecision precision) => Ring (Fixed precision)
29−(KnownPrecision precision) => CommutativeRing (Fixed precision)
35−fromInt :: forall precision. KnownPrecision precision => Int -> Fixed precision
38−Create a `Fixed` representation of an `Int`.
43−fromNumber :: forall precision. KnownPrecision precision => Number -> Maybe (Fixed precision)
46−Approximate a `Number` as a `Fixed` value with the specified precision.
49−> fromNumber 0.1234 :: Maybe (Fixed P10000)
50−(Just (fromNumber 0.1234 :: P10000))
52−> fromNumber 0.1234 :: Maybe (Fixed P100)
53−(Just (fromNumber 0.12 :: P100))
56−When given a finite `Number`, this function always succeeds: the number is
57−truncated (rounded towards zero) to the closest possible `Fixed` value.
58−This function only returns `Nothing` if it is given NaN, or positive or
62−> fromNumber (1.0 / 0.0) :: Maybe (Fixed P100)
69−toNumber :: forall precision. KnownPrecision precision => Fixed precision -> Number
72−Convert a `Fixed` value to a `Number`.
74−_Note_: Overflow is possible here if the numerator is sufficiently large.
75−Consider using `toString` instead.
80−fromString :: forall precision. KnownPrecision precision => String -> Maybe (Fixed precision)
83−Parse a fixed-precision number from a string. Any decimal digits which are
84−not representable in the specified precision will be ignored.
87−> fromString "123.456" :: Maybe (Fixed P1000)
88−(Just (fromString "123.456" :: P1000))
91−Where possible, this function should be preferred over `fromNumber`, since
92−it is exact (whereas `fromNumber` can only provide an approximation for
96−> fromString "9007199254740992.5" :: Maybe (Fixed P10)
97−(Just (fromString "9007199254740992.5" :: P10))
99−> fromNumber 9007199254740992.5 :: Maybe (Fixed P10)
100−(Just (fromString "9007199254740992.0" :: P10))
107−toString :: forall precision. KnownPrecision precision => Fixed precision -> String
110−Represent a `Fixed` value as a string, using all of the decimal places it
111−can represent (based on its precision).
114−> map toString (fromString "100.5" :: Maybe (Fixed P10))
117−> map toString (fromString "100.5" :: Maybe (Fixed P100))
121−#### `toStringWithPrecision`
124−toStringWithPrecision :: forall precision. KnownPrecision precision => Int -> Fixed precision -> String
127−Represent a `Fixed` value as a string, with the given number of decimal
131−> map (toStringWithPrecision 2) (fromString "1234.567" :: Maybe (Fixed P1000))
135−If more decimal places are asked for than the type can provide, the extra
136−decimal places will be provided as zeroes.
139−> map (toStringWithPrecision 3) (fromString "1234.5" :: Maybe (Fixed P10))
146−numerator :: forall precision. Fixed precision -> BigInt
149−Extract the numerator from the representation of the number as a fraction.
152−> map numerator (fromNumber 0.1234 :: Fixed P1000)
153−(Just fromString "123")
155−> map numerator (fromNumber 0.1239 :: Fixed P1000)
156−(Just fromString "123")
162−floor :: forall precision. KnownPrecision precision => Fixed precision -> Fixed precision
165−Calculate the largest whole number smaller than or equal to the provided
169−> map floor $ fromNumber 0.1 :: Maybe (Fixed P10)
170−(Just (fromNumber 0.0 :: P10))
172−> map floor $ fromNumber 1.0 :: Maybe (Fixed P10)
173−(Just (fromNumber 1.0 :: P10))
175−> floor $ fromNumber (-0.1) :: Maybe (Fixed P10)
176−(Just (fromNumber (-1.0) :: P10))
182−ceil :: forall precision. KnownPrecision precision => Fixed precision -> Fixed precision
185−Calculate the smallest whole number greater than or equal to the provided
189−> map ceil $ fromNumber 0.1 :: Maybe (Fixed P10)
190−(Just (fromNumber 1.0 :: P10))
192−> map ceil $ fromNumber 1.0 :: Maybe (Fixed P10)
193−(Just (fromNumber 1.0 :: P10))
195−> map ceil $ fromNumber (-0.1) :: Maybe (Fixed P10)
196−(Just (fromNumber 0.0 :: P10))
202−round :: forall precision. KnownPrecision precision => Fixed precision -> Fixed precision
205−Round the specified value to the nearest whole number.
208−> map round $ fromNumber 0.1 :: Maybe (Fixed P10)
209−(Just (fromNumber 0.0 :: P10))
211−> map round $ fromNumber 0.9 :: Maybe (Fixed P10)
212−(Just (fromNumber 1.0 :: P10))
214−> map round $ fromNumber 0.5 :: Maybe (Fixed P10)
215−(Just (fromNumber 1.0 :: P10))
217−> map round $ fromNumber (-0.1) :: Maybe (Fixed P10)
218−(Just (fromNumber 0.0 :: P10))
224−approxDiv :: forall precision. KnownPrecision precision => Fixed precision -> Fixed precision -> Fixed precision
227−Approximate division of fixed-precision numbers.
230−> lift2 approxDiv (fromNumber 22.0) (fromNumber 7.0) :: Maybe (Fixed P100)
231−(Just (fromNumber 3.14 :: P100))
234−_Note_: `Fixed` is not a `EuclideanRing` in general - it is not even
235−an integral domain, since it has non-zero zero-divisors:
238−> lift2 (*) (fromNumber 0.1) (fromNumber 0.1) :: Maybe (Fixed P10)
239−(Just (fromNumber 0.0 :: P10))
248−A kind for type-level precision information
253−data One :: Precision
266−data TenTimes :: Precision -> Precision
269−One more decimal place
273−(KnownPrecision p) => KnownPrecision (TenTimes p)
285−type P10 = TenTimes P1
293−type P100 = TenTimes P10
301−type P1000 = TenTimes P100
309−type P10000 = TenTimes P1000
317−type P100000 = TenTimes P10000
325−type P1000000 = TenTimes P100000
333−data PProxy (precision :: Precision)
337−A value-level proxy for a type-level precision.
339−#### `KnownPrecision`
342−class KnownPrecision (precision :: Precision) where
343− reflectPrecision :: PProxy precision -> BigInt
346−Precision which is known, i.e. it can be reflected to a
347−value at runtime, given a `PProxy`.
349−`reflectPrecision` returns a multiple of ten, corresponding
350−to the maximum number of decimal places which can be stored.
353−> reflectPrecision (PProxy :: PProxy P1000)
360−(KnownPrecision p) => KnownPrecision (TenTimes p)
363−#### `reflectPrecisionDecimalPlaces`
366−reflectPrecisionDecimalPlaces :: forall precision. KnownPrecision precision => PProxy precision -> Int
369−Get the number of decimal places associated with a given `Precision` at
373−> reflectPrecisionDecimalPlaces (PProxy :: PProxy P1000)
377−#### `reifyPrecision`
380−reifyPrecision :: forall r. Int -> (forall precision. KnownPrecision precision => PProxy precision -> r) -> Maybe r
383−Reify an non-negative integer (a power of ten) as a `Precision`.
388−> reifyPrecision 0 reflectPrecision
390−> reifyPrecision 1 reflectPrecision
392−> reifyPrecision 2 reflectPrecision
394−> reifyPrecision (-1) reflectPrecision