From 3bcfb0624aff1c53969a4fde2e005eaea5a38a23 Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 29 Mar 2007 22:09:25 +0000 Subject: [PATCH] Add soft-coded utilites to the "io" and "math" modules. The math stuff is, for the most part, already available in the geo module; but it's small and this naming matches the evolvoing "standard nasal library". --- Nasal/io.nas | 8 ++++++++ Nasal/math.nas | 15 +++++++++++++++ Nasal/props.nas | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Nasal/io.nas create mode 100644 Nasal/math.nas diff --git a/Nasal/io.nas b/Nasal/io.nas new file mode 100644 index 000000000..fdd65ead3 --- /dev/null +++ b/Nasal/io.nas @@ -0,0 +1,8 @@ +# Reads and returns a complete file as a string +var readfile = func(file) { + if((var st = stat(file)) == nil) die("Cannot stat file: " ~ file); + var sz = st[7]; + var buf = bits.buf(sz); + read(open(file), buf, sz); + return buf; +} diff --git a/Nasal/math.nas b/Nasal/math.nas new file mode 100644 index 000000000..554329aaa --- /dev/null +++ b/Nasal/math.nas @@ -0,0 +1,15 @@ +var abs = func(n) { n < 0 ? -n : n } + +var pow = func(x, y) { exp(y * ln(x)) } + +var mod = func(n, m) { + var x = n - m * int(n/m); # int() truncates to zero, not -Inf + return x < 0 ? x + abs(m) : x; # ...so must handle negative n's +} + +var asin = func(y) { atan2(y, sqrt(1-y*y)) } + +var acos = func(x) { atan2(sqrt(1-x*x), x) } + +var _iln10 = 1/ln(10); +var log10 = func(x) { ln(x) * _iln10; } diff --git a/Nasal/props.nas b/Nasal/props.nas index fb94d8b81..7ce608dde 100644 --- a/Nasal/props.nas +++ b/Nasal/props.nas @@ -164,7 +164,7 @@ var wrapNode = func { { parents : [Node], _g : arg[0] } } # Does anything ever call globals.set_props() from C++? May need to # turn this into a function if so. # -props.globals = wrapNode(_globals()); +var globals = wrapNode(_globals()); ## # Sets all indexed property children to a single value. arg[0]