Difference between revisions of "Cookbook/Numbers"
< Cookbook
Jump to navigation
Jump to search
m (Fixed broken links) |
|||
(7 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
Numbers in Haskell can be of the type <hask>Int, Integer, Float, Double, or Rational</hask>. |
Numbers in Haskell can be of the type <hask>Int, Integer, Float, Double, or Rational</hask>. |
||
− | = Rounding numbers = |
+ | == Rounding numbers == |
{| class="wikitable" |
{| class="wikitable" |
||
Line 9: | Line 9: | ||
! Examples |
! Examples |
||
|- |
|- |
||
− | | rounding |
+ | | rounding a given number |
− | | [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:round round] |
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v:round round] |
|<haskell> |
|<haskell> |
||
round 3.4 --> 3 |
round 3.4 --> 3 |
||
round 3.5 --> 4 |
round 3.5 --> 4 |
||
+ | round 2.5 --> 2 |
||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | finding the nearest integer greater than or equal to |
+ | | finding the nearest integer greater than or equal to a given number |
− | | [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aceiling ceiling] |
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3Aceiling ceiling] |
|<haskell> |
|<haskell> |
||
ceiling 3.0 --> 3 |
ceiling 3.0 --> 3 |
||
Line 23: | Line 24: | ||
</haskell> |
</haskell> |
||
|- |
|- |
||
− | | finding the nearest integer less than or equal to |
+ | | finding the nearest integer less than or equal to a given number |
− | | [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Afloor floor] |
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3Afloor floor] |
|<haskell> |
|<haskell> |
||
floor 3.0 --> 3 |
floor 3.0 --> 3 |
||
floor 3.9 --> 3 |
floor 3.9 --> 3 |
||
+ | </haskell> |
||
+ | |- |
||
+ | | finding the nearest integer between zero and a given number |
||
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Prelude.html#v%3Atruncate truncate] |
||
+ | |<haskell> |
||
+ | truncate 3.0 --> 3 |
||
+ | truncate 3.9 --> 3 |
||
+ | truncate (negate 3.0) --> -3 |
||
+ | truncate (negate 3.9) --> -3 |
||
</haskell> |
</haskell> |
||
|} |
|} |
||
− | =Taking logarithms = |
+ | == Taking logarithms == |
<haskell> |
<haskell> |
||
log 2.718281828459045 --> 1.0 |
log 2.718281828459045 --> 1.0 |
||
− | logBase 10 10000 --> 4.0 |
+ | logBase-4.12.0.0 10 10000 --> 4.0 |
</haskell> |
</haskell> |
||
− | = Generating random numbers = |
+ | == Generating random numbers == |
<haskell> |
<haskell> |
||
import System.Random |
import System.Random |
||
Line 47: | Line 57: | ||
</haskell> |
</haskell> |
||
− | = Binary representation of numbers = |
+ | == Binary representation of numbers == |
<haskell> |
<haskell> |
||
import Data.Bits |
import Data.Bits |
||
⚫ | |||
-- Extract a range of bits, most-significant first |
-- Extract a range of bits, most-significant first |
||
bitRange :: Bits a => a -> Int -> Int -> [Bool] |
bitRange :: Bits a => a -> Int -> Int -> [Bool] |
||
− | bitRange n lo hi = |
+ | bitRange n lo hi = reverse . map (testBit n) [lo..hi] |
-- Extract all bits, most-significant first |
-- Extract all bits, most-significant first |
||
Line 66: | Line 75: | ||
</haskell> |
</haskell> |
||
− | = Using complex numbers = |
+ | == Using complex numbers == |
{| class="wikitable" |
{| class="wikitable" |
||
Line 75: | Line 84: | ||
|- |
|- |
||
| creating a complex number from real and imaginary rectangular components |
| creating a complex number from real and imaginary rectangular components |
||
− | | [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Complex.html#v%3A%3A%2B (:+)] |
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Data-Complex.html#v%3A%3A%2B (:+)] |
|<haskell> |
|<haskell> |
||
− | import Complex |
+ | import Data.Complex |
1.0 :+ 0.0 --> 1.0 :+ 0.0 |
1.0 :+ 0.0 --> 1.0 :+ 0.0 |
||
</haskell> |
</haskell> |
||
|- |
|- |
||
| creating a complex number from polar components |
| creating a complex number from polar components |
||
− | | [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Complex.html#v%3AmkPolar mkPolar] |
+ | | [http://haskell.org/ghc/docs/latest/html/libraries/base-4.12.0.0/Data-Complex.html#v%3AmkPolar mkPolar] |
|<haskell> |
|<haskell> |
||
− | import Complex |
+ | import Data.Complex |
mkPolar 1.0 pi --> (-1.0) :+ 1.2246063538223773e-16 |
mkPolar 1.0 pi --> (-1.0) :+ 1.2246063538223773e-16 |
||
+ | </haskell> |
||
+ | |- |
||
+ | | adding complex numbers |
||
+ | | |
||
+ | |<haskell> |
||
⚫ | |||
+ | (1 :+ 1) + (2 :+ 2) --> 3.0 :+ 3.0 |
||
</haskell> |
</haskell> |
||
|} |
|} |
Latest revision as of 21:03, 6 January 2019
Numbers in Haskell can be of the type Int, Integer, Float, Double, or Rational
.
Rounding numbers
Problem | Solution | Examples |
---|---|---|
rounding a given number | round | round 3.4 --> 3
round 3.5 --> 4
round 2.5 --> 2
|
finding the nearest integer greater than or equal to a given number | ceiling | ceiling 3.0 --> 3
ceiling 3.1 --> 4
|
finding the nearest integer less than or equal to a given number | floor | floor 3.0 --> 3
floor 3.9 --> 3
|
finding the nearest integer between zero and a given number | truncate | truncate 3.0 --> 3
truncate 3.9 --> 3
truncate (negate 3.0) --> -3
truncate (negate 3.9) --> -3
|
Taking logarithms
log 2.718281828459045 --> 1.0
logBase-4.12.0.0 10 10000 --> 4.0
Generating random numbers
import System.Random
main = do
gen <- getStdGen
let ns = randoms gen :: [Int]
print $ take 10 ns
Binary representation of numbers
import Data.Bits
-- Extract a range of bits, most-significant first
bitRange :: Bits a => a -> Int -> Int -> [Bool]
bitRange n lo hi = reverse . map (testBit n) [lo..hi]
-- Extract all bits, most-significant first
bits :: Bits a => a -> [Bool]
bits n = bitRange n 0 (bitSize n - 1)
-- Display a number in binary, including leading zeroes.
-- c.f. Numeric.showHex
showBits :: Bits a => a -> ShowS
showBits = showString . map (\b -> if b then '1' else '0') . bits
Using complex numbers
Problem | Solution | Examples |
---|---|---|
creating a complex number from real and imaginary rectangular components | (:+) | import Data.Complex
1.0 :+ 0.0 --> 1.0 :+ 0.0
|
creating a complex number from polar components | mkPolar | import Data.Complex
mkPolar 1.0 pi --> (-1.0) :+ 1.2246063538223773e-16
|
adding complex numbers | import Data.Complex
(1 :+ 1) + (2 :+ 2) --> 3.0 :+ 3.0
|