divMod' alternative for faster calculations
Solution 1:
It looks like the library function is defined in this way:
-- | Generalisation of 'div' to any instance of 'Real'
div' :: (Real a,Integral b) => a -> a -> b
div' n d = floor ((toRational n) / (toRational d))
-- | Generalisation of 'divMod' to any instance of 'Real'
divMod' :: (Real a,Integral b) => a -> a -> (b,a)
divMod' n d = (f,n - (fromIntegral f) * d) where
f = div' n d
You could adapt that to Double
and simplify it, at the cost of some approximation:
divDouble :: Double -> Double -> Int
divDouble n d = floor (n / d)
{-#INLINABLE divDouble #-}
divDoubleMod :: Double -> Double -> (Int,Double)
divDoubleMod n d = (f,n - (fromIntegral f) * d) where
f = divDouble n d
{-#INLINABLE divDoubleMod #-}
Hopefully, Double
division is faster than the original division on Rational
, even if it's more approximated.