| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
NumHask.Examples
Description
NumHask usage examples
Synopsis
Imports and Pragmas
NumHask.Prelude is a replacement for the standard prelude with the NoImplicitPrelude extension explicitly required.
>>>:set -XNoImplicitPrelude>>>:set -XFlexibleContexts>>>:set -XRebindableSyntax>>>import NumHask.Prelude
Int, Integer, Double and Float are from base. NumHask takes these classes and redefines the basic arithmetic operators.
>>>1 + 12>>>1 - 10>>>1 * 11
>>>1.0 / 1.01.0
Note that the literal numbers in the divide above defaulted to Float rather than Int.
>>>1 / (1::Int)... ... No instance for (Divisive Int) ... ...
>>>1.0 / fromInteger 11.0
RebindableSyntax removes the Haskell98 link between literal numbers and prelude. Literal numbers are pre-pended by ghc with fromInteger for integers and fromRational for decimals.
>>>:set -XNoRebindableSyntax>>>:t 11 :: Num p => p
>>>:t 1.01.0 :: Fractional p => p
>>>:set -XRebindableSyntax>>>:t 11 :: FromInteger a => a
>>>:t 1.01.0 :: FromRational a => a
Float and Double are Field instances.
>>>zero == 0.0True>>>one == 1.0True>>>1.0 + 1.02.0>>>1.0 - 1.00.0>>>1.0 * 1.01.0>>>1.0 / 1.01.0
>>>1 `div` 20>>>3 `mod` 21
BoundedField
>>>1.0/0.0Infinity>>>-1.0/0.0-Infinity>>>0.0/0.0 + 1.0NaN
>>>one/zeroInfinity
>>>-one/zero-Infinity
>>>zero/zero+oneNaN
>>>logBase 2 42.0>>>2 ** 24.0>>>sqrt 42.0>>>exp 27.38905609893065>>>log 20.6931471805599453
Complex numbers
>>>let a = 1 :+ 2>>>a1 :+ 2>>>zero - a(-1) :+ (-2)>>>(1 :+ (-2)) * ((-2) :+ 4)6 :+ 8
>>>(1.0 :+ (-1.0)) / (2.0 :+ 2.0)0.0 :+ (-0.5)