module Algebra.RealRing98 where

{-# INLINE fastSplitFraction #-}
fastSplitFraction :: (RealFrac a, Integral b) =>
   (a -> Int) -> (Int -> a) -> a -> (b,a)
fastSplitFraction :: (a -> Int) -> (Int -> a) -> a -> (b, a)
fastSplitFraction a -> Int
trunc Int -> a
toFloat a
x =
   (b, a) -> (b, a)
forall a b. (Num a, Num b, Ord a) => (b, a) -> (b, a)
fixSplitFraction ((b, a) -> (b, a)) -> (b, a) -> (b, a)
forall a b. (a -> b) -> a -> b
$
   if Int -> a
toFloat Int
forall a. Bounded a => a
minBound a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> a
toFloat Int
forall a. Bounded a => a
maxBound
     then case a -> Int
trunc a
x of Int
n -> (Int -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n, a
x a -> a -> a
forall a. Num a => a -> a -> a
- Int -> a
toFloat Int
n)
     else case a -> (Integer, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x of (Integer
n,a
f) -> (Integer -> b
forall a. Num a => Integer -> a
fromInteger Integer
n, a
f)

{-# INLINE fixSplitFraction #-}
fixSplitFraction :: (Num a, Num b, Ord a) => (b,a) -> (b,a)
fixSplitFraction :: (b, a) -> (b, a)
fixSplitFraction (b
n,a
f) =
   --  if x>=0 || f==0
   if a
fa -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
0
     then (b
n,   a
f)
     else (b
nb -> b -> b
forall a. Num a => a -> a -> a
-b
1, a
fa -> a -> a
forall a. Num a => a -> a -> a
+a
1)


{-# INLINE fastFraction #-}
fastFraction :: (RealFrac a) => (a -> a) -> a -> a
fastFraction :: (a -> a) -> a -> a
fastFraction a -> a
trunc a
x =
   a -> a
forall a. Real a => a -> a
fixFraction (a -> a) -> a -> a
forall a b. (a -> b) -> a -> b
$
   if Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
forall a. Bounded a => a
minBound :: Int) a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
x Bool -> Bool -> Bool
&& a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
forall a. Bounded a => a
maxBound :: Int)
     then a
x a -> a -> a
forall a. Num a => a -> a -> a
- a -> a
trunc a
x
     else a -> a
forall a. RealFrac a => a -> a
signedFraction a
x

{-# INLINE signedFraction #-}
signedFraction :: (RealFrac a) => a -> a
signedFraction :: a -> a
signedFraction a
x =
   let second :: (Integer, a) -> a
       second :: (Integer, a) -> a
second = (Integer, a) -> a
forall a b. (a, b) -> b
snd
   in  (Integer, a) -> a
forall a. (Integer, a) -> a
second (a -> (Integer, a)
forall a b. (RealFrac a, Integral b) => a -> (b, a)
properFraction a
x)

{-# INLINE fixFraction #-}
fixFraction :: (Real a) => a -> a
fixFraction :: a -> a
fixFraction a
y =
   if a
ya -> a -> Bool
forall a. Ord a => a -> a -> Bool
>=a
0 then a
y else a
ya -> a -> a
forall a. Num a => a -> a -> a
+a
1