fix bounds + update libs
This commit is contained in:
parent
828ebb23c0
commit
bc58da6d49
5 changed files with 59 additions and 50 deletions
|
|
@ -501,7 +501,7 @@ func (p *Parser) binary() (Node, error) {
|
||||||
op := tokenToBinaryOperation(ops.Pop().Type)
|
op := tokenToBinaryOperation(ops.Pop().Type)
|
||||||
|
|
||||||
start, _ := l.Bounds()
|
start, _ := l.Bounds()
|
||||||
_, end := l.Bounds()
|
_, end := r.Bounds()
|
||||||
|
|
||||||
values.Push(&BinaryNode{
|
values.Push(&BinaryNode{
|
||||||
op,
|
op,
|
||||||
|
|
|
||||||
|
|
@ -7,19 +7,19 @@
|
||||||
terms := 1000000
|
terms := 1000000
|
||||||
|
|
||||||
# The running sum of terms
|
# The running sum of terms
|
||||||
tot := 0
|
tot := 0.0
|
||||||
|
|
||||||
n := 1
|
n := 1
|
||||||
while n <= terms {
|
while n <= terms {
|
||||||
tot = tot + 1 / (n*n)
|
tot = tot + 1.0/float(n*n)
|
||||||
n = n + 1
|
n = n + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
tot = tot * 6
|
tot = tot * 6.0
|
||||||
|
|
||||||
# get the absolute value of a number
|
# get the absolute value of a number
|
||||||
func abs(x: number) number {
|
fn abs(x: float) -> float {
|
||||||
if x < 0 {
|
if x < 0.0 {
|
||||||
return -x
|
return -x
|
||||||
}
|
}
|
||||||
return x
|
return x
|
||||||
|
|
@ -30,19 +30,19 @@ func abs(x: number) number {
|
||||||
# see: https://en.wikipedia.org/wiki/Newton's_method
|
# see: https://en.wikipedia.org/wiki/Newton's_method
|
||||||
# The required accuracy
|
# The required accuracy
|
||||||
SQRT_ACC := 0.00000001
|
SQRT_ACC := 0.00000001
|
||||||
func sqrt(x: number) number {
|
fn sqrt(x: float) -> float {
|
||||||
pg := 0 # previous guess
|
pg := 0.0 # previous guess
|
||||||
g := 1 # current guess
|
g := 1.0 # current guess
|
||||||
|
|
||||||
while abs(pg - g) >= SQRT_ACC {
|
while abs(pg - g) >= SQRT_ACC {
|
||||||
pg = g
|
pg = g
|
||||||
g = (pg + tot/pg)/2
|
g = (pg + x/pg)/2.0
|
||||||
}
|
}
|
||||||
|
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
tot = sqrt(tot)
|
pi := sqrt(tot)
|
||||||
|
|
||||||
# output the result
|
# output the result
|
||||||
write(str(tot))
|
println(pi)
|
||||||
|
|
|
||||||
|
|
@ -13,3 +13,5 @@ _slow_blink := CSI + "5m"
|
||||||
_rapid_blink := CSI + "6m"
|
_rapid_blink := CSI + "6m"
|
||||||
_strike := CSI + "9m"
|
_strike := CSI + "9m"
|
||||||
_primary_font := CSI + "10m"
|
_primary_font := CSI + "10m"
|
||||||
|
|
||||||
|
fn red(s: string) -> string {}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
func map(list: list[any], f: func(any)any) list[any] {
|
fn map(list: [any], f: fn(any) -> any) -> [any] {
|
||||||
out := []
|
out := []
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
|
|
|
||||||
81
lib/math.ang
81
lib/math.ang
|
|
@ -7,7 +7,7 @@ E := 2.718281828459045235360287471352
|
||||||
# Get the absolute value of a number. If x is negative, the returned
|
# Get the absolute value of a number. If x is negative, the returned
|
||||||
# value is positive and equal to `-x`. If x is positive or zero, the
|
# value is positive and equal to `-x`. If x is positive or zero, the
|
||||||
# returned value is x.
|
# returned value is x.
|
||||||
func abs(x: number) number {
|
fn absf(x: float) -> float {
|
||||||
# if the number is negative
|
# if the number is negative
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
# negate it so it's positive
|
# negate it so it's positive
|
||||||
|
|
@ -17,15 +17,23 @@ func abs(x: number) number {
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn absi(n: int) -> int {
|
||||||
|
if n < 0 {
|
||||||
|
-n
|
||||||
|
} else {
|
||||||
|
n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DERIVE_DX := 0.00000001
|
DERIVE_DX := 0.00000001
|
||||||
func derive(f: func(number)number, x: number) number {
|
fn derive(f: fn(float) -> float, x: float) float {
|
||||||
return (f(x + DERIVE_DX) - f(x))/DERIVE_DX
|
return (f(x + DERIVE_DX) - f(x))/DERIVE_DX
|
||||||
}
|
}
|
||||||
|
|
||||||
NEWTONS_ACC := 0.000000000001
|
NEWTONS_ACC := 0.000000000001
|
||||||
func newtons(f: func(number)number) number {
|
fn newtons(f: fn(float) -> float) -> float {
|
||||||
pg := 0
|
pg := 0.0
|
||||||
g := 1
|
g := 1.0
|
||||||
|
|
||||||
while abs(g - pg) > NEWTONS_ACC {
|
while abs(g - pg) > NEWTONS_ACC {
|
||||||
pg = g
|
pg = g
|
||||||
|
|
@ -41,9 +49,9 @@ MAX_SQRT_DX := 0.0000001
|
||||||
# x: number
|
# x: number
|
||||||
# Calculate the approximate square root using newton's method until
|
# Calculate the approximate square root using newton's method until
|
||||||
# the accuracy has increased by less than the variable `MAX_SQRT_DX`.
|
# the accuracy has increased by less than the variable `MAX_SQRT_DX`.
|
||||||
func sqrt(x: number) number {
|
fn sqrt(x: float) -> float {
|
||||||
ng := x
|
ng := x
|
||||||
g := 1
|
g := 1.0
|
||||||
|
|
||||||
while abs(g - ng) > MAX_SQRT_DX {
|
while abs(g - ng) > MAX_SQRT_DX {
|
||||||
g = ng
|
g = ng
|
||||||
|
|
@ -51,8 +59,6 @@ func sqrt(x: number) number {
|
||||||
# create new guess
|
# create new guess
|
||||||
ng = (g + x / g) / 2
|
ng = (g + x / g) / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
return g
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# floor(x)
|
# floor(x)
|
||||||
|
|
@ -72,7 +78,7 @@ func sqrt(x: number) number {
|
||||||
# round(x)
|
# round(x)
|
||||||
# x: number
|
# x: number
|
||||||
# Return the closest whole number to the value x.
|
# Return the closest whole number to the value x.
|
||||||
func round(x: number) number {
|
fn round(x: float) -> float {
|
||||||
f := floor(x)
|
f := floor(x)
|
||||||
|
|
||||||
if x - f > 0.5 {
|
if x - f > 0.5 {
|
||||||
|
|
@ -86,7 +92,7 @@ func round(x: number) number {
|
||||||
# x: number; any number
|
# x: number; any number
|
||||||
# n: number; the number to divide by
|
# n: number; the number to divide by
|
||||||
# Return the rest from a division of x by n.
|
# Return the rest from a division of x by n.
|
||||||
func mod(x: number, n: number) number {
|
fn mod(x: float, n: float) -> float {
|
||||||
if x == 0 {
|
if x == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
@ -110,17 +116,18 @@ func mod(x: number, n: number) number {
|
||||||
# This value is only reasonable if 0<x<1.
|
# This value is only reasonable if 0<x<1.
|
||||||
# It is approximated using the taylor series of e**x.
|
# It is approximated using the taylor series of e**x.
|
||||||
SM_EXP_ACC := 0.00000000001
|
SM_EXP_ACC := 0.00000000001
|
||||||
func sm_exp(x: number) number {
|
fn sm_exp(x: float) -> float {
|
||||||
p_tot := 0
|
p_tot := 0.0
|
||||||
tot := 1
|
tot := 1.0
|
||||||
n := 1
|
n := 1
|
||||||
x_pow := x
|
x_pow := x
|
||||||
f := 1
|
f := 1.0
|
||||||
|
|
||||||
while abs(tot - p_tot) > SM_EXP_ACC {
|
while abs(tot - p_tot) > SM_EXP_ACC {
|
||||||
p_tot = tot
|
p_tot = tot
|
||||||
t := x_pow / f
|
t := x_pow / f
|
||||||
tot = tot + t
|
tot = tot + t
|
||||||
f = f * (n+1)
|
f = f * float(n+1)
|
||||||
x_pow = x_pow * x
|
x_pow = x_pow * x
|
||||||
n = n + 1
|
n = n + 1
|
||||||
}
|
}
|
||||||
|
|
@ -131,22 +138,22 @@ func sm_exp(x: number) number {
|
||||||
# exp(x)
|
# exp(x)
|
||||||
# x: number; any number
|
# x: number; any number
|
||||||
# Get an approximate value of e raised to the power of x.
|
# Get an approximate value of e raised to the power of x.
|
||||||
func exp(x: number) number {
|
fn exp(x: float) -> float {
|
||||||
n := abs(x)
|
n := abs(x)
|
||||||
tot := 1
|
tot := 1.0
|
||||||
while n >= 1 {
|
while n >= 1 {
|
||||||
tot = tot * E
|
tot = tot * E
|
||||||
n = n - 1
|
n = n - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if n > 0 {
|
if n > 0.0 {
|
||||||
tot = tot * sm_exp(n)
|
tot = tot * sm_exp(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
return 1/tot
|
1.0/tot
|
||||||
} else {
|
} else {
|
||||||
return tot
|
tot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,9 +162,9 @@ func exp(x: number) number {
|
||||||
# Get the approximate value of the natural logarithm
|
# Get the approximate value of the natural logarithm
|
||||||
# This function uses newton's method to approximate.
|
# This function uses newton's method to approximate.
|
||||||
LN_ACC := 0.0000000001
|
LN_ACC := 0.0000000001
|
||||||
func ln(x: number) number {
|
fn ln(x: float) -> float {
|
||||||
pg := 0
|
pg := 0.0
|
||||||
g := 1
|
g := 1.0
|
||||||
|
|
||||||
while abs(pg - g) > LN_ACC {
|
while abs(pg - g) > LN_ACC {
|
||||||
pg = g
|
pg = g
|
||||||
|
|
@ -171,7 +178,7 @@ func ln(x: number) number {
|
||||||
# x: number; any number. The base
|
# x: number; any number. The base
|
||||||
# p: number; the value of the exponent
|
# p: number; the value of the exponent
|
||||||
# Raise any number to any power (x^p)
|
# Raise any number to any power (x^p)
|
||||||
func pow(x: number, p: number) number {
|
fn pow(x: float, p: float) -> float {
|
||||||
return exp(p*ln(x))
|
return exp(p*ln(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -181,15 +188,15 @@ func pow(x: number, p: number) number {
|
||||||
# Calculate the approximate value of the logarithm
|
# Calculate the approximate value of the logarithm
|
||||||
# of a with b as base.
|
# of a with b as base.
|
||||||
LOG_ACC := 0.0000001
|
LOG_ACC := 0.0000001
|
||||||
func log(a: number, b: number) number {
|
fn log(a: float, b: float) -> float {
|
||||||
ln_b := ln(b)
|
ln_b := ln(b)
|
||||||
|
|
||||||
pg := 0
|
pg := 0.0
|
||||||
g := 1
|
g := 1.0
|
||||||
|
|
||||||
while abs(g - pg) > LOG_ACC {
|
while abs(g - pg) > LOG_ACC {
|
||||||
pg = g
|
pg = g
|
||||||
g = pg - 1/ln_b - a/(ln_b*pow(b, pg))
|
g = pg - 1.0/ln_b - a/(ln_b*pow(b, pg))
|
||||||
}
|
}
|
||||||
|
|
||||||
return g
|
return g
|
||||||
|
|
@ -199,9 +206,9 @@ func log(a: number, b: number) number {
|
||||||
# x: number; an angle in radians
|
# x: number; an angle in radians
|
||||||
# Get the sine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
# Get the sine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
||||||
# TODO: use hashmap with precomputed values and linear interpolation
|
# TODO: use hashmap with precomputed values and linear interpolation
|
||||||
func sin(x: number) number {
|
fn sin(x: float) -> float {
|
||||||
f := 1
|
f := 1.0
|
||||||
x = mod(x, 2*PI)
|
x = mod(x, 2.0*PI)
|
||||||
if x > PI {
|
if x > PI {
|
||||||
x = PI - x
|
x = PI - x
|
||||||
f = -1
|
f = -1
|
||||||
|
|
@ -209,13 +216,13 @@ func sin(x: number) number {
|
||||||
|
|
||||||
# compute sine with a taylor series mock function of sine (valid between -pi and +pi)
|
# compute sine with a taylor series mock function of sine (valid between -pi and +pi)
|
||||||
tot := x
|
tot := x
|
||||||
l := 1
|
l := 1.0
|
||||||
i := 1
|
i := 1.0
|
||||||
s := -1
|
s := -1.0
|
||||||
|
|
||||||
while i <= 19 {
|
while i <= 19 {
|
||||||
i = i + 2
|
i = i + 2.0
|
||||||
l = s * l * x / i / (i-1)
|
l = s * l * x / i / (i-1.0)
|
||||||
|
|
||||||
tot = tot + l
|
tot = tot + l
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue