aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 16:44:45 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 16:44:45 -0500
commitc5f315e338adb09bcfc4c192ccefedfaf88202fe (patch)
tree2bbabc5d7d079cb0496522592d97cb75deee50bb
parent74c86176cb2bc68f7a3c7b9c65d93184c8b7d959 (diff)
Num methods
-rw-r--r--builtins/nums.c10
-rw-r--r--environment.c23
-rw-r--r--test/integers.tm9
-rw-r--r--test/nums.tm32
4 files changed, 70 insertions, 4 deletions
diff --git a/builtins/nums.c b/builtins/nums.c
index 456da93a..26bc1457 100644
--- a/builtins/nums.c
+++ b/builtins/nums.c
@@ -46,13 +46,17 @@ public double Num__mod(double num, double modulus) {
return (result < 0) != (modulus < 0) ? result + modulus : result;
}
+public double Num__random(void) {
+ return drand48();
+}
+
public double Num__nan(CORD tag) {
return nan(CORD_to_const_char_star(tag));
}
-public bool Num__isinf(double n) { return isinf(n); }
-public bool Num__finite(double n) { return finite(n); }
-public bool Num__isnan(double n) { return isnan(n); }
+public bool Num__isinf(double n) { return !!isinf(n); }
+public bool Num__finite(double n) { return !!finite(n); }
+public bool Num__isnan(double n) { return !!isnan(n); }
public const TypeInfo Num = {
.size=sizeof(double),
diff --git a/environment.c b/environment.c
index 1145e029..e4b1c26d 100644
--- a/environment.c
+++ b/environment.c
@@ -100,7 +100,28 @@ env_t *new_compilation_unit(void)
{"min", "Int8__min", "Int8"},
{"max", "Int8__max", "Int8"},
)},
- {"Num", Type(NumType, .bits=64), "Num_t", "Num", {}},
+#define C(name) {#name, "Num__"#name, "Num"}
+#define F(name) {#name, "Num__"#name, "func(n:Num)->Num"}
+#define F2(name) {#name, "Num__"#name, "func(x:Num, y:Num)->Num"}
+ {"Num", Type(NumType, .bits=64), "Num_t", "Num", $TypedArray(ns_entry_t,
+ {"format", "Num__format", "func(n:Num, precision=0)->Str"},
+ {"scientific", "Num__scientific", "func(n:Num, precision=0)->Str"},
+ {"nan", "Num__nan", "func(tag=\"\")->Num"},
+ {"isinf", "Num__isinf", "func(n:Num)->Bool"},
+ {"isfinite", "Num__isfinite", "func(n:Num)->Bool"},
+ {"isnan", "Num__isnan", "func(n:Num)->Bool"},
+ C(2_SQRTPI), C(E), C(PI_2), C(2_PI), C(1_PI), C(LN10), C(LN2), C(LOG2E),
+ C(PI), C(PI_4), C(SQRT2), C(SQRT1_2), C(INF), C(TAU),
+ {"random", "Num__random", "func()->Num"},
+ F(abs), F(acos), F(acosh), F(asin), F(asinh), F(atan), F(atanh), F(cbrt), F(ceil), F(cos), F(cosh), F(erf), F(erfc),
+ F(exp), F(exp2), F(expm1), F(floor), F(j0), F(j1), F(log), F(log10), F(log1p), F(log2), F(logb),
+ F(rint), F(round), F(significand), F(sin), F(sinh), F(sqrt),
+ F(tan), F(tanh), F(tgamma), F(trunc), F(y0), F(y1),
+ F2(atan2), F2(copysign), F2(fdim), F2(hypot), F2(nextafter), F2(pow), F2(remainder),
+ )},
+#undef F2
+#undef F
+#undef C
{"Num32", Type(NumType, .bits=32), "Num32_t", "Num32", {}},
{"Str", Type(StringType), "Str_t", "Str", $TypedArray(ns_entry_t,
{"quoted", "Str__quoted", "func(s:Str, color=no)->Str"},
diff --git a/test/integers.tm b/test/integers.tm
index 2570521a..a3ec7fe2 100644
--- a/test/integers.tm
+++ b/test/integers.tm
@@ -33,6 +33,8 @@ for x in 5
= "1,2,3,4,5,"
>> x := 123
+>> x:format(digits=5)
+= "00123"
>> x:hex()
= "0x7B"
>> x:octal()
@@ -44,3 +46,10 @@ for x in 5
>> Int.max
= 9223372036854775807
+
+>> 123_i32:hex()
+= "0x7B"
+>> 123_i16:hex()
+= "0x7B"
+>> 123_i8:hex()
+= "0x7B"
diff --git a/test/nums.tm b/test/nums.tm
new file mode 100644
index 00000000..c5b19325
--- /dev/null
+++ b/test/nums.tm
@@ -0,0 +1,32 @@
+>> n := 1.5
+= 1.5
+
+>> n + n
+= 3
+
+>> n * 2
+= 3
+
+>> n - n
+= 0
+
+>> Num.PI
+= 3.14159
+
+>> Num.PI:format(precision=10)
+= "3.1415926536"
+
+>> Num.random()
+
+>> Num.INF
+= inf
+>> Num.INF:isinf()
+= yes
+
+>> Num.nan()
+= nan
+>> nan := Num.nan()
+>> nan:isnan()
+= yes
+>> nan == nan
+= no