Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)