fix bounds + update libs

This commit is contained in:
Neemek 2026-07-11 10:13:49 +02:00
parent 828ebb23c0
commit bc58da6d49
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
5 changed files with 59 additions and 50 deletions

View file

@ -501,7 +501,7 @@ func (p *Parser) binary() (Node, error) {
op := tokenToBinaryOperation(ops.Pop().Type)
start, _ := l.Bounds()
_, end := l.Bounds()
_, end := r.Bounds()
values.Push(&BinaryNode{
op,

View file

@ -7,19 +7,19 @@
terms := 1000000
# The running sum of terms
tot := 0
tot := 0.0
n := 1
while n <= terms {
tot = tot + 1 / (n*n)
tot = tot + 1.0/float(n*n)
n = n + 1
}
tot = tot * 6
tot = tot * 6.0
# get the absolute value of a number
func abs(x: number) number {
if x < 0 {
fn abs(x: float) -> float {
if x < 0.0 {
return -x
}
return x
@ -30,19 +30,19 @@ func abs(x: number) number {
# see: https://en.wikipedia.org/wiki/Newton's_method
# The required accuracy
SQRT_ACC := 0.00000001
func sqrt(x: number) number {
pg := 0 # previous guess
g := 1 # current guess
fn sqrt(x: float) -> float {
pg := 0.0 # previous guess
g := 1.0 # current guess
while abs(pg - g) >= SQRT_ACC {
pg = g
g = (pg + tot/pg)/2
g = (pg + x/pg)/2.0
}
return g
}
tot = sqrt(tot)
pi := sqrt(tot)
# output the result
write(str(tot))
println(pi)

View file

@ -13,3 +13,5 @@ _slow_blink := CSI + "5m"
_rapid_blink := CSI + "6m"
_strike := CSI + "9m"
_primary_font := CSI + "10m"
fn red(s: string) -> string {}

View file

@ -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 := []
i := 0

View file

@ -7,7 +7,7 @@ E := 2.718281828459045235360287471352
# 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
# returned value is x.
func abs(x: number) number {
fn absf(x: float) -> float {
# if the number is negative
if x < 0 {
# negate it so it's positive
@ -17,15 +17,23 @@ func abs(x: number) number {
return x
}
fn absi(n: int) -> int {
if n < 0 {
-n
} else {
n
}
}
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
}
NEWTONS_ACC := 0.000000000001
func newtons(f: func(number)number) number {
pg := 0
g := 1
fn newtons(f: fn(float) -> float) -> float {
pg := 0.0
g := 1.0
while abs(g - pg) > NEWTONS_ACC {
pg = g
@ -41,9 +49,9 @@ MAX_SQRT_DX := 0.0000001
# x: number
# Calculate the approximate square root using newton's method until
# the accuracy has increased by less than the variable `MAX_SQRT_DX`.
func sqrt(x: number) number {
fn sqrt(x: float) -> float {
ng := x
g := 1
g := 1.0
while abs(g - ng) > MAX_SQRT_DX {
g = ng
@ -51,8 +59,6 @@ func sqrt(x: number) number {
# create new guess
ng = (g + x / g) / 2
}
return g
}
# floor(x)
@ -72,7 +78,7 @@ func sqrt(x: number) number {
# round(x)
# x: number
# Return the closest whole number to the value x.
func round(x: number) number {
fn round(x: float) -> float {
f := floor(x)
if x - f > 0.5 {
@ -86,7 +92,7 @@ func round(x: number) number {
# x: number; any number
# n: number; the number to divide by
# 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 {
return 0
}
@ -110,17 +116,18 @@ func mod(x: number, n: number) number {
# This value is only reasonable if 0<x<1.
# It is approximated using the taylor series of e**x.
SM_EXP_ACC := 0.00000000001
func sm_exp(x: number) number {
p_tot := 0
tot := 1
fn sm_exp(x: float) -> float {
p_tot := 0.0
tot := 1.0
n := 1
x_pow := x
f := 1
f := 1.0
while abs(tot - p_tot) > SM_EXP_ACC {
p_tot = tot
t := x_pow / f
tot = tot + t
f = f * (n+1)
f = f * float(n+1)
x_pow = x_pow * x
n = n + 1
}
@ -131,22 +138,22 @@ func sm_exp(x: number) number {
# exp(x)
# x: number; any number
# 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)
tot := 1
tot := 1.0
while n >= 1 {
tot = tot * E
n = n - 1
}
if n > 0 {
if n > 0.0 {
tot = tot * sm_exp(n)
}
if x < 0 {
return 1/tot
1.0/tot
} else {
return tot
tot
}
}
@ -155,9 +162,9 @@ func exp(x: number) number {
# Get the approximate value of the natural logarithm
# This function uses newton's method to approximate.
LN_ACC := 0.0000000001
func ln(x: number) number {
pg := 0
g := 1
fn ln(x: float) -> float {
pg := 0.0
g := 1.0
while abs(pg - g) > LN_ACC {
pg = g
@ -171,7 +178,7 @@ func ln(x: number) number {
# x: number; any number. The base
# p: number; the value of the exponent
# 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))
}
@ -181,15 +188,15 @@ func pow(x: number, p: number) number {
# Calculate the approximate value of the logarithm
# of a with b as base.
LOG_ACC := 0.0000001
func log(a: number, b: number) number {
fn log(a: float, b: float) -> float {
ln_b := ln(b)
pg := 0
g := 1
pg := 0.0
g := 1.0
while abs(g - pg) > LOG_ACC {
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
@ -199,9 +206,9 @@ func log(a: number, b: number) number {
# x: number; an angle in radians
# 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
func sin(x: number) number {
f := 1
x = mod(x, 2*PI)
fn sin(x: float) -> float {
f := 1.0
x = mod(x, 2.0*PI)
if x > PI {
x = PI - x
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)
tot := x
l := 1
i := 1
s := -1
l := 1.0
i := 1.0
s := -1.0
while i <= 19 {
i = i + 2
l = s * l * x / i / (i-1)
i = i + 2.0
l = s * l * x / i / (i-1.0)
tot = tot + l