2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,11 @@
|
|||
open number
|
||||
|
||||
_ ^ 0 = 1
|
||||
x ^ n | n > 0 = f x (n - 1) x
|
||||
|else = fail "Negative exponent"
|
||||
where f _ 0 y = y
|
||||
f a d y = g a d
|
||||
where g b i | even i = g (b * b) (i `quot` 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
(12 ^ 4, 12 ** 4)
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
open number
|
||||
|
||||
//Function quot from number module is defined only for
|
||||
//integral numbers. We can use this as an universal quot.
|
||||
uquot x y | x is Integral = x `quot` y
|
||||
| else = x / y
|
||||
|
||||
//Changing implementation by using generic numeric literals
|
||||
//(e.g. 2u) and elimitating all comparisons with 0.
|
||||
!x ^ n | n ~= 0u = 1u
|
||||
| n > 0u = f x (n - 1u) x
|
||||
| else = fail "Negative exponent"
|
||||
where f a d y
|
||||
| d ~= 0u = y
|
||||
| else = g a d
|
||||
where g b i | even i = g (b * b) (i `uquot` 2u)
|
||||
| else = f b (i - 1u) (b * y)
|
||||
|
||||
|
||||
(12 ^ 4, 12.34 ^ 4.04)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
open number
|
||||
|
||||
//A class that defines our overloadable function
|
||||
class Exponent a where
|
||||
(^) a->a->_
|
||||
|
||||
//Implementation for integers
|
||||
instance Exponent Int where
|
||||
_ ^ 0 = 1
|
||||
x ^ n | n > 0 = f x (n - 1) x
|
||||
|else = fail "Negative exponent"
|
||||
where f _ 0 y = y
|
||||
f a d y = g a d
|
||||
where g b i | even i = g (b * b) (i `quot` 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
//Implementation for floats
|
||||
instance Exponent Single where
|
||||
x ^ n | n < 0.001 = 1
|
||||
| n > 0 = f x (n - 1) x
|
||||
| else = fail "Negative exponent"
|
||||
where f a d y
|
||||
| d < 0.001 = y
|
||||
| else = g a d
|
||||
where g b i | even i = g (b * b) (i / 2)
|
||||
| else = f b (i - 1) (b * y)
|
||||
|
||||
(12 ^ 4, 12.34 ^ 4.04)
|
||||
Loading…
Add table
Add a link
Reference in a new issue