packages feed

papa-0.7.0: src/Papa/Unsafe/Numeric.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | The partial numeric functions of base that "Papa" replaces, under their
-- own names and types. Each can throw, or silently lose information. The
-- total replacement in "Papa" is named in each group.
--
-- This module exports only these functions, so it combines with "Papa":
--
-- > import Papa
-- > import qualified Papa.Unsafe.Numeric as Unsafe
--
-- "Papa.Unsafe" is "Papa" with every partial function, these included.
--
-- @since 0.7.0
module Papa.Unsafe.Numeric (
  -- * Integral division (total: 'Papa.Base.Data.Integral.divNonZero', ...)

  -- | Throw for a divisor of zero, and for @minBound \`div\` (-1)@ on a
  -- bounded type.
  div,
  mod,
  divMod,
  quot,
  rem,
  quotRem,

  -- * Exponentiation (total: 'Papa.Base.Data.Integral.pow')

  -- | Throws for a negative exponent.
  (^),

  -- * Fractional division (total: 'Papa.Base.Data.Fractional.divideNonZero', ...; at a floating type, 'Papa.Base.Data.RealFloat.divideFloating', 'Papa.Base.Data.RealFloat.recipFloating')

  -- | Throw for a divisor of zero at 'Data.Ratio.Ratio'.
  (/),
  recip,
  (^^),

  -- * Ratios (total: 'Papa.Base.Data.Ratio.ratioNonZero')

  -- | Throws for a denominator of zero.
  (%),

  -- * Integral conversion (total: 'Papa.Base.Data.IntCast.intCast', 'Papa.Base.Data.Integral.toIntegralSized')

  -- | Wraps around where the value does not fit in the target type
  -- (@fromIntegral (300 :: Int) :: Word8@ is @44@), and throws converting a
  -- negative number to 'Numeric.Natural.Natural'.
  fromIntegral,

  -- * Conversion from fractional types (total: 'Papa.Base.Data.RealFrac.truncateMaybe', ...; to the same floating type, 'Papa.Base.Data.RealFloat.floorFloating', ...)

  -- | Return an arbitrary number for @NaN@ and the infinities of 'Prelude.Double'
  -- and 'Prelude.Float', and wrap around where the result does not fit in the target
  -- type (@truncate (300.5 :: Double) :: Word8@ is @44@).
  truncate,
  round,
  ceiling,
  floor,
  properFraction,
  toRational,
  -- | Returns an arbitrary number for @NaN@ and the infinities
  -- (total: 'Papa.Base.Data.RealFrac.approxRationalMaybe').
  approxRational,

  -- * Conversion between fractional types (total: 'Papa.Base.Data.RealFrac.floatToDouble', 'Papa.Base.Data.RealFrac.doubleToFloat', 'Papa.Base.Data.RealFrac.toRationalMaybe')

  -- | Goes through 'Prelude.Rational', which has no @NaN@ or infinity: at
  -- @Double -> Double@ it may turn @NaN@ into a number, depending on
  -- optimisation.
  realToFrac,

  -- * Conversion from 'Prelude.Integer' (total: 'Papa.Base.Data.IntCast.intCast', 'Papa.Base.Data.Integral.toIntegralSized')

  -- | Wraps around where the value does not fit in the target type
  -- (@fromInteger 300 :: Word8@ is @44@), and throws for a negative number
  -- at 'Natural'. "Papa" does not export it, but numeric literals still
  -- work: GHC calls it for them whether or not it is in scope, and warns
  -- for a literal that does not fit (@-Woverflowed-literals@).
  fromInteger,

  -- * Natural numbers

  -- | The 'Prelude.Num' instance of 'Natural' throws @arithmetic underflow@ for
  -- a negative result: of @-@, 'Prelude.negate' and 'fromInteger'
  -- (@1 - 2 :: Natural@, and the literal @-1 :: Natural@, which is
  -- @'Prelude.negate' 1@). 'minusNaturalMaybe' subtracts without throwing.
  -- 'Papa.Base.Data.Integral.pow' takes a 'Data.Word.Word' exponent.
  Natural,
  minusNaturalMaybe,

  -- * Ratios of any integral type

  -- | A 'Ratio' of a bounded type such as 'Data.Int.Int' wraps around on
  -- overflow, and so returns a wrong number (@'Prelude.negate' (minBound %
  -- 1 :: Ratio Int)@ is @minBound % 1@). "Papa" exports 'Prelude.Rational',
  -- @Ratio Integer@, which cannot overflow.
  Ratio,
) where

import Data.Ratio (Ratio, approxRational, (%))
import Numeric.Natural (Natural, minusNaturalMaybe)
import Prelude (ceiling, div, divMod, floor, fromInteger, fromIntegral, mod, properFraction, quot, quotRem, realToFrac, recip, rem, round, toRational, truncate, (/), (^), (^^))