Karya, built on 2023-08-29T07:47:28 (patch 7a412d5d6ba4968ca4155ef276a062ccdeb9109a)
Safe HaskellNone

Util.Num

Description

Miscellaneous functions on numbers. Things that could have gone in Numeric.

Synopsis

show

binary :: Bits a => a -> String Source #

Show a word as binary.

Warning: for some reason, Integer is an instance of Bits, but bitSize will crash.

hex :: (Integral a, Show a) => Int -> a -> Text Source #

zeroPad :: Show a => Int -> a -> Text Source #

showFloat :: RealFloat a => Int -> a -> Text Source #

Display a float with the given precision, dropping trailing and leading zeros. Haskell requires a 0 before the decimal point, so this produces non-Haskell numbers.

showFloatP :: RealFloat a => Bool -> Int -> a -> Text Source #

Fancy decimal float formatting. Show up to the given precision, like showFloat, unless it would be all 0s. In that case, show the minimum necessary to get to a non-zero.

showFloat0 :: RealFloat a => Maybe Int -> a -> Text Source #

Like showFloat, but use a leading 0, so haskell can parse it.

assert

assertDiv :: Stack.HasCallStack => Integral a => a -> a -> a Source #

Like div, but assert that it divides evenly.

read

transform

sum :: (Foldable t, Num a) => t a -> a Source #

Prelude sum is lazy, for silly reasons.

roundDigits :: (RealFrac a, Fractional b) => Int -> a -> b Source #

roundUp :: (Integral factor, Real a) => factor -> a -> factor Source #

Round up to the nearest factor above the given number.

roundDown :: (Integral factor, Real a) => factor -> a -> factor Source #

clamp :: Ord a => a -> a -> a -> a Source #

Clamp a value to be between low and high.

restrict :: Real a => a -> a -> a -> a Source #

Confine the given value lie between the first two arguments, but using modulus, not clamping.

inRange :: Ord a => a -> a -> a -> Bool Source #

scale :: (Eq a, Num a) => a -> a -> a -> a Source #

Scale v, which is between 0 and 1 inclusive, to be between low and high. If v is not in the 0--1 range, the result will be out of the low--high range.

This is linear interpolate, or lerp.

normalize :: (Eq a, Fractional a) => a -> a -> a -> a Source #

Normalize v, which is between low and high inclusive, to be between 0 and 1. As with scale, if v is not in range, the result will not be in range either.

fmod :: Real a => a -> a -> a infixl 7 Source #

fmod is in a bizarre place.

fDivMod :: (Integral div, RealFrac mod) => mod -> mod -> (div, mod) Source #

asIntegral :: (RealFrac frac, Integral int) => frac -> Maybe int Source #

d2f :: Double -> Float Source #

realToFrac doesn't preserve the special float values and is inefficient.

There are some RULEs for this, but they aren't reliable.

d2i :: Double -> Int Source #

Conversion that clamps at INT_MIN / INT_MAX.