papa-0.7.0: src/Papa/Base/Data/RealFloat.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Total floating-point division and rounding, whose results are
-- floating-point numbers.
--
-- At a floating-point type ('Prelude.Double', 'Prelude.Float'), division by
-- zero does not throw: IEEE 754 defines it as an infinity, or @NaN@ for
-- @0 / 0@. "Prelude"'s @(/)@ is in "Papa.Unsafe.Numeric" because it throws
-- at 'Prelude.Rational'. 'divideFloating' and 'recipFloating' are @(/)@ and
-- @recip@ at 'RealFloat' only, so they need no
-- 'Papa.Base.Data.NonZero.NonZero' divisor, and do not compile at
-- 'Prelude.Rational'.
--
-- 'floorFloating', 'ceilingFloating', 'truncateFloating' and 'roundFloating'
-- round to a whole number of the same type, as C's @floor@ and the like do.
-- They are total: @NaN@ and the infinities are their own results, and so is
-- every number too large to have a fractional part. Where the result is
-- zero, its sign is that of the argument (@ceilingFloating (-0.5)@ is
-- @-0.0@). "Papa.Base.Data.RealFrac"'s 'Papa.Base.Data.RealFrac.floorMaybe'
-- and the like convert to an integral type instead.
--
-- 'modFloating' is "Data.Fixed"'s @mod'@, which throws for a divisor of
-- zero, made total.
--
-- These functions assume that the 'RealFloat' instance is IEEE 754
-- arithmetic, as those of base are.
--
-- @since 0.7.0
module Papa.Base.Data.RealFloat (
divideFloating,
recipFloating,
floorFloating,
ceilingFloating,
truncateFloating,
roundFloating,
modFloating,
) where
import Data.Bool (bool, not, (&&), (||))
import Data.Eq (Eq ((==)))
import Data.Ord (Ord ((<), (>), (>=)))
import Prelude (Integer, Num (abs, negate, (*), (-)), RealFloat (encodeFloat, floatDigits, isInfinite, isNaN, isNegativeZero), RealFrac, toRational)
import qualified Prelude as P
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Data.Fixed as Fixed
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genDouble = Gen.double (Range.exponentialFloatFrom 0 (-1e30) 1e30)
-- >>> let genSmall = Gen.double (Range.linearFracFrom 0 (-1000) 1000)
-- | Division at a floating-point type. Division by zero is an infinity, or
-- @NaN@ for @0 / 0@, as IEEE 754 defines it. "Prelude"'s @(/)@, which throws
-- at 'Prelude.Rational', is in "Papa.Unsafe.Numeric"; for a 'Prelude.Rational'
-- use 'Papa.Base.Data.Fractional.divideNonZero'.
--
-- ==== __Examples__
--
-- >>> divideFloating 1 4 :: Double
-- 0.25
--
-- >>> divideFloating 1 0 :: Double
-- Infinity
--
-- >>> isNaN (divideFloating 0 0 :: Double)
-- True
--
-- >>> check $ withTests 200 $ property $ do x <- forAll genDouble; y <- forAll (Gen.filter (/= 0) genDouble); Just (divideFloating x y) === fmap (divideNonZero x) (preview nonZero y)
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
divideFloating :: (RealFloat a) => a -> a -> a
divideFloating =
(P./)
{-# INLINE divideFloating #-}
-- | The reciprocal at a floating-point type: an infinity for zero.
--
-- ==== __Examples__
--
-- >>> recipFloating 4 :: Double
-- 0.25
--
-- >>> recipFloating (-0) :: Double
-- -Infinity
--
-- >>> check $ withTests 200 $ property $ do x <- forAll genDouble; recipFloating x === divideFloating 1 x
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
recipFloating :: (RealFloat a) => a -> a
recipFloating =
P.recip
{-# INLINE recipFloating #-}
-- | Round with the given function to an 'Integer', where the value has a
-- fractional part. A value whose magnitude is at least
-- @'floatRadix' ^ ('floatDigits' - 1)@ (2^52 at 'Prelude.Double') has
-- none, and nor have @NaN@ and the infinities: each is its own result. Below
-- that, the rounded 'Integer' converts back exactly. A zero result takes the
-- sign of the argument.
wholeWith :: (RealFloat a) => (a -> Integer) -> a -> a
wholeWith f x =
let r = P.fromInteger (f x)
in bool
(bool r (negate 0) (r == 0 && (x < 0 || isNegativeZero x)))
x
(isNaN x || isInfinite x || abs x >= encodeFloat 1 (floatDigits x P.- 1))
{-# INLINE wholeWith #-}
-- | The greatest whole number not greater than the argument, at the
-- argument's type. @NaN@ and the infinities are their own results.
--
-- ==== __Examples__
--
-- >>> floorFloating 2.5 :: Double
-- 2.0
--
-- >>> floorFloating (-2.5) :: Double
-- -3.0
--
-- >>> floorFloating (-0.0) :: Double
-- -0.0
--
-- >>> floorFloating 1e300 :: Double
-- 1.0e300
--
-- >>> check $ withTests 500 $ property $ do x <- forAll genDouble; Just (floorFloating x) === fmap integralToFloating (floorMaybe x :: Maybe Integer)
-- ...passed 500 tests.
-- True
--
-- @since 0.7.0
floorFloating :: (RealFloat a) => a -> a
floorFloating =
wholeWith P.floor
{-# INLINE floorFloating #-}
-- | The least whole number not less than the argument, at the argument's
-- type. @NaN@ and the infinities are their own results.
--
-- ==== __Examples__
--
-- >>> ceilingFloating 2.5 :: Double
-- 3.0
--
-- >>> ceilingFloating (-0.5) :: Double
-- -0.0
--
-- >>> check $ withTests 500 $ property $ do x <- forAll genDouble; Just (ceilingFloating x) === fmap integralToFloating (ceilingMaybe x :: Maybe Integer)
-- ...passed 500 tests.
-- True
--
-- @since 0.7.0
ceilingFloating :: (RealFloat a) => a -> a
ceilingFloating =
wholeWith P.ceiling
{-# INLINE ceilingFloating #-}
-- | The argument without its fractional part (rounded towards zero), at the
-- argument's type. @NaN@ and the infinities are their own results.
--
-- ==== __Examples__
--
-- >>> truncateFloating 2.7 :: Double
-- 2.0
--
-- >>> truncateFloating (-2.7) :: Double
-- -2.0
--
-- >>> check $ withTests 500 $ property $ do x <- forAll genDouble; Just (truncateFloating x) === fmap integralToFloating (truncateMaybe x :: Maybe Integer)
-- ...passed 500 tests.
-- True
--
-- @since 0.7.0
truncateFloating :: (RealFloat a) => a -> a
truncateFloating =
wholeWith P.truncate
{-# INLINE truncateFloating #-}
-- | The nearest whole number, at the argument's type; a half rounds to the
-- even number, as "Prelude"'s @round@ does. @NaN@ and the infinities are
-- their own results.
--
-- ==== __Examples__
--
-- >>> roundFloating 2.5 :: Double
-- 2.0
--
-- >>> roundFloating 3.5 :: Double
-- 4.0
--
-- >>> divideFloating (roundFloating (2.345 * 100)) 100 :: Double
-- 2.35
--
-- >>> check $ withTests 500 $ property $ do x <- forAll genDouble; Just (roundFloating x) === fmap integralToFloating (roundMaybe x :: Maybe Integer)
-- ...passed 500 tests.
-- True
--
-- @since 0.7.0
roundFloating :: (RealFloat a) => a -> a
roundFloating =
wholeWith P.round
{-# INLINE roundFloating #-}
-- | The remainder of floored division, at a floating-point type: the
-- remainder has the sign of the divisor, as 'Papa.Base.Data.Integral.modNonZero'
-- does, so that @modFloating a 360@ is an angle from 0 to 360.
--
-- The remainder is computed exactly and then rounded, so it is the
-- floating-point number nearest to the true remainder. That number can be
-- the divisor itself, where the true remainder is less than the divisor by
-- less than its precision: @modFloating (-1e-20) 360@ is @360.0@.
--
-- 'Data.Fixed.mod'' throws for a divisor of zero. Here:
--
-- * a divisor of zero or @NaN@, or a dividend of @NaN@ or an infinity, gives
-- @NaN@, as C's @fmod@ does;
-- * an infinite divisor gives the dividend where the two have the same sign
-- (or the dividend is zero), and the divisor where they do not, which are
-- the limits of the remainder.
--
-- ==== __Examples__
--
-- >>> modFloating 370 360 :: Double
-- 10.0
--
-- >>> modFloating (-90) 360 :: Double
-- 270.0
--
-- >>> modFloating 7.5 (-2) :: Double
-- -0.5
--
-- >>> isNaN (modFloating 1 0 :: Double)
-- True
--
-- >>> check $ withTests 500 $ property $ do x <- forAll genSmall; y <- forAll (Gen.filter (/= 0) genSmall); modFloating x y === P.fromRational (Fixed.mod' (P.toRational x) (P.toRational y))
-- ...passed 500 tests.
-- True
--
-- >>> check $ withTests 500 $ property $ do a <- forAll (Gen.integral (Range.linear (-1000) 1000)); b <- forAll (Gen.filter (/= 0) (Gen.integral (Range.linear (-1000) 1000))); modFloating (integralToFloating a) (integralToFloating b) === (integralToFloating (maybe 0 (modNonZero a) (preview nonZero (b :: Integer))) :: Double)
-- ...passed 500 tests.
-- True
--
-- @since 0.7.0
modFloating :: (RealFloat a) => a -> a -> a
modFloating x y
| isNaN x || isInfinite x || isNaN y || y == 0 =
0 P./ 0
| isInfinite y =
bool y x (x == 0 || (x > 0) == (y > 0))
| P.otherwise =
let r = P.fromRational (exactMod x y)
in bool r (negate 0) (r == 0 && y < 0)
{-# INLINEABLE modFloating #-}
-- | The remainder of floored division, as a 'Prelude.Rational', exactly.
-- The divisor is finite and not zero.
exactMod :: (RealFrac a) => a -> a -> P.Rational
exactMod x y =
let rx = toRational x
ry = toRational y
in rx - ry * P.fromInteger (P.floor (rx P./ ry))
{-# INLINE exactMod #-}
-- Bring 'not' into use for HLint-free guards elsewhere.
_unused :: P.Bool -> P.Bool
_unused = not