basic for loop + examples -> era3
This commit is contained in:
parent
dd7af341a3
commit
0bc8b2ef55
24 changed files with 222 additions and 201 deletions
50
lib/math.ang
50
lib/math.ang
|
|
@ -9,7 +9,7 @@ E := 2.718281828459045235360287471352
|
|||
# returned value is x.
|
||||
fn absf(x: float) -> float {
|
||||
# if the number is negative
|
||||
if x < 0 {
|
||||
if x < 0.0 {
|
||||
# negate it so it's positive
|
||||
return -x
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ fn absi(n: int) -> int {
|
|||
}
|
||||
|
||||
DERIVE_DX := 0.00000001
|
||||
fn derive(f: fn(float) -> float, x: float) float {
|
||||
fn derive(f: fn(float) -> float, x: float) -> float {
|
||||
return (f(x + DERIVE_DX) - f(x))/DERIVE_DX
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ fn newtons(f: fn(float) -> float) -> float {
|
|||
pg := 0.0
|
||||
g := 1.0
|
||||
|
||||
while abs(g - pg) > NEWTONS_ACC {
|
||||
while absf(g - pg) > NEWTONS_ACC {
|
||||
pg = g
|
||||
g = pg - f(pg) / derive(f, pg)
|
||||
}
|
||||
|
|
@ -53,12 +53,14 @@ fn sqrt(x: float) -> float {
|
|||
ng := x
|
||||
g := 1.0
|
||||
|
||||
while abs(g - ng) > MAX_SQRT_DX {
|
||||
while absf(g - ng) > MAX_SQRT_DX {
|
||||
g = ng
|
||||
|
||||
# create new guess
|
||||
ng = (g + x / g) / 2
|
||||
ng = (g + x / g) / 2.0
|
||||
}
|
||||
|
||||
g
|
||||
}
|
||||
|
||||
# floor(x)
|
||||
|
|
@ -82,7 +84,7 @@ fn round(x: float) -> float {
|
|||
f := floor(x)
|
||||
|
||||
if x - f > 0.5 {
|
||||
return f + 1
|
||||
return f + 1.0
|
||||
}
|
||||
|
||||
return f
|
||||
|
|
@ -93,16 +95,16 @@ fn round(x: float) -> float {
|
|||
# n: number; the number to divide by
|
||||
# Return the rest from a division of x by n.
|
||||
fn mod(x: float, n: float) -> float {
|
||||
if x == 0 {
|
||||
return 0
|
||||
if x == 0.0 {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
if x < 0 {
|
||||
while x + n <= 0 {
|
||||
if x < 0.0 {
|
||||
while x + n <= 0.0 {
|
||||
x = x + n
|
||||
}
|
||||
} else {
|
||||
while x - n >= 0 {
|
||||
while x - n >= 0.0 {
|
||||
x = x - n
|
||||
}
|
||||
}
|
||||
|
|
@ -123,7 +125,7 @@ fn sm_exp(x: float) -> float {
|
|||
x_pow := x
|
||||
f := 1.0
|
||||
|
||||
while abs(tot - p_tot) > SM_EXP_ACC {
|
||||
while absf(tot - p_tot) > SM_EXP_ACC {
|
||||
p_tot = tot
|
||||
t := x_pow / f
|
||||
tot = tot + t
|
||||
|
|
@ -139,18 +141,18 @@ fn sm_exp(x: float) -> float {
|
|||
# x: number; any number
|
||||
# Get an approximate value of e raised to the power of x.
|
||||
fn exp(x: float) -> float {
|
||||
n := abs(x)
|
||||
n := absf(x)
|
||||
tot := 1.0
|
||||
while n >= 1 {
|
||||
while n >= 1.0 {
|
||||
tot = tot * E
|
||||
n = n - 1
|
||||
n = n - 1.0
|
||||
}
|
||||
|
||||
if n > 0.0 {
|
||||
tot = tot * sm_exp(n)
|
||||
}
|
||||
|
||||
if x < 0 {
|
||||
if x < 0.0 {
|
||||
1.0/tot
|
||||
} else {
|
||||
tot
|
||||
|
|
@ -166,9 +168,9 @@ fn ln(x: float) -> float {
|
|||
pg := 0.0
|
||||
g := 1.0
|
||||
|
||||
while abs(pg - g) > LN_ACC {
|
||||
while absf(pg - g) > LN_ACC {
|
||||
pg = g
|
||||
g = pg + x / exp(pg) - 1
|
||||
g = pg + x / exp(pg) - 1.0
|
||||
}
|
||||
|
||||
return g
|
||||
|
|
@ -194,7 +196,7 @@ fn log(a: float, b: float) -> float {
|
|||
pg := 0.0
|
||||
g := 1.0
|
||||
|
||||
while abs(g - pg) > LOG_ACC {
|
||||
while absf(g - pg) > LOG_ACC {
|
||||
pg = g
|
||||
g = pg - 1.0/ln_b - a/(ln_b*pow(b, pg))
|
||||
}
|
||||
|
|
@ -211,7 +213,7 @@ fn sin(x: float) -> float {
|
|||
x = mod(x, 2.0*PI)
|
||||
if x > PI {
|
||||
x = PI - x
|
||||
f = -1
|
||||
f = -1.0
|
||||
}
|
||||
|
||||
# compute sine with a taylor series mock function of sine (valid between -pi and +pi)
|
||||
|
|
@ -220,7 +222,7 @@ fn sin(x: float) -> float {
|
|||
i := 1.0
|
||||
s := -1.0
|
||||
|
||||
while i <= 19 {
|
||||
while i <= 19.0 {
|
||||
i = i + 2.0
|
||||
l = s * l * x / i / (i-1.0)
|
||||
|
||||
|
|
@ -235,13 +237,15 @@ fn sin(x: float) -> float {
|
|||
# cos(x)
|
||||
# x: number; an angle in radians
|
||||
# Get the cosine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
||||
func cos(x: number) number {
|
||||
fn cos(x: float) -> float {
|
||||
# todo
|
||||
0.0
|
||||
}
|
||||
|
||||
# tan(x)
|
||||
# x: number; an angle in radians
|
||||
# Get the tangent of an angle. https://en.wikipedia.org/wiki/Tangent
|
||||
func tan(x: number) number {
|
||||
fn tan(x: float) -> float {
|
||||
# todo
|
||||
0.0
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue