@@ -11,6 +11,7 @@ module Data.Fixed
14 , kind Precision
15 , kind Precision
16 , TenTimes
17 , TenTimes
@@ -234,6 +235,32 @@ round n = Fixed (numerator n + x) where
234 | m * BigInt.fromInt 2 >= d = d - m
235 | m * BigInt.fromInt 2 >= d = d - m
235 | otherwise = -m
236 | otherwise = -m
238+-- | Approximate division of fixed-precision numbers.
241+-- | > fromNumber 22.0 `approxDiv` fromNumber 7.0 :: Fixed P100
242+-- | fromNumber 3.14 :: P100
245+-- | _Note_: `Fixed` is not a `EuclideanRing` in general - it is not even
246+-- | an integral domain, since it has non-zero zero-divisors:
249+-- | > fromNumber 0.1 * fromNumber 0.1 :: Fixed P10
250+-- | fromNumber 0.0 :: P10
254+ . KnownPrecision precision
258+approxDiv a b = Fixed (x * n / y)
262+ n = BigInt.fromInt (denominator a)
237instance showFixed :: KnownPrecision precision => Show (Fixed precision) where
264instance showFixed :: KnownPrecision precision => Show (Fixed precision) where
239 "(fromNumber "
266 "(fromNumber "
@@ -257,17 +284,4 @@ instance semiringFixed :: KnownPrecision precision => Semiring (Fixed precision)
257instance ringFixed :: KnownPrecision precision => Ring (Fixed precision) where
284instance ringFixed :: KnownPrecision precision => Ring (Fixed precision) where
258 sub (Fixed n) (Fixed m) = Fixed (n - m)
285 sub (Fixed n) (Fixed m) = Fixed (n - m)
260−instance euclideanRingFixed :: KnownPrecision precision => EuclideanRing (Fixed precision) where
261− div a b = Fixed (x * n / y)
265− n = BigInt.fromInt (denominator a)
266− mod a b = Fixed (((x * n) `mod` y) / n)
270− n = BigInt.fromInt (denominator a)
271− degree = degree <<< numerator
273instance commutativeRingFixed :: KnownPrecision precision => CommutativeRing (Fixed precision)
287instance commutativeRingFixed :: KnownPrecision precision => CommutativeRing (Fixed precision)