plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
ce5dd16
— Peter John
2026-09-08T19:12:41+05:30
docs(examples): drop unnecessary braces from numeric literal method calls
- plum-examples/numbers.plum +31 -31
plum-examples/numbers.plum
CHANGED
|
@@ -18,62 +18,62 @@ test "abs works for both Int and Float, wrapping back into the same variant"
|
|
|
18
18
|
assert {-2.5}.abs().kind() == "Float"
|
|
19
19
|
|
|
20
20
|
test "sign returns -1/0/1 (or the matching Float) for both Int and Float"
|
|
21
|
-
assert toInt(
|
|
21
|
+
assert toInt(5.sign()) == 1
|
|
22
22
|
assert toInt({-5}.sign()) == -1
|
|
23
|
-
assert toInt(
|
|
23
|
+
assert toInt(0.sign()) == 0
|
|
24
24
|
|
|
25
25
|
test "hash is identity for Int and truncating for Float"
|
|
26
|
-
assert
|
|
26
|
+
assert 5.hash() == 5
|
|
27
|
-
assert
|
|
27
|
+
assert 5.9.hash() == 5
|
|
28
28
|
|
|
29
29
|
test "trunc/floor/ceil/round agree with Int passthrough and real Float math"
|
|
30
|
-
assert
|
|
30
|
+
assert 5.trunc() == 5.0f
|
|
31
|
-
assert
|
|
31
|
+
assert 2.7.trunc() == 2.0f
|
|
32
32
|
assert {-2.7}.trunc() == -2.0f
|
|
33
|
-
assert
|
|
33
|
+
assert 2.7.floor() == 2.0f
|
|
34
34
|
assert {-2.7}.floor() == -3.0f
|
|
35
|
-
assert
|
|
35
|
+
assert 2.3.ceil() == 3.0f
|
|
36
36
|
assert {-2.3}.ceil() == -2.0f
|
|
37
|
-
assert
|
|
37
|
+
assert 2.5.round() == 3.0f
|
|
38
38
|
assert {-2.5}.round() == -3.0f
|
|
39
39
|
|
|
40
40
|
test "sqrt works for Int (via Float conversion) and Float directly"
|
|
41
|
-
assert
|
|
41
|
+
assert 4.sqrt() == 2.0f
|
|
42
|
-
assert
|
|
42
|
+
assert 2.25.sqrt() == 1.5f
|
|
43
43
|
|
|
44
44
|
test "pow computes integer powers exactly for both Int and Float self"
|
|
45
|
-
assert
|
|
45
|
+
assert 2.pow(10.0f) == 1024.0f
|
|
46
|
-
assert
|
|
46
|
+
assert 2.0.pow(10.0f) == 1024.0f
|
|
47
47
|
|
|
48
48
|
test "log2/log10 agree between Int and the equivalent Float"
|
|
49
49
|
# `log`/`log2`/`log10` are built on the Taylor-series `ln` approximation
|
|
50
50
|
# (see `plum-std/Number.plum`'s `ln`), not exact libm — compare within a
|
|
51
51
|
# small epsilon rather than requiring bit-exact equality.
|
|
52
|
-
a :=
|
|
52
|
+
a := 8.log2()
|
|
53
|
-
b :=
|
|
53
|
+
b := 8.0.log2()
|
|
54
54
|
assert {a - b}.abs().toFloatValue() < 0.0001f
|
|
55
|
-
c :=
|
|
55
|
+
c := 100.log10()
|
|
56
|
-
d :=
|
|
56
|
+
d := 100.0.log10()
|
|
57
57
|
assert {c - d}.abs().toFloatValue() < 0.0001f
|
|
58
58
|
|
|
59
59
|
test "isFinite/isInfinite/isNaN are always well-defined for Int, real for Float"
|
|
60
|
-
assert
|
|
60
|
+
assert 5.isFinite()
|
|
61
|
-
assert !
|
|
61
|
+
assert !5.isInfinite()
|
|
62
|
-
assert !
|
|
62
|
+
assert !5.isNaN()
|
|
63
63
|
assert {1.0f / 0.0f}.isInfinite()
|
|
64
64
|
assert {0.0f / 0.0f}.isNaN()
|
|
65
|
-
assert
|
|
65
|
+
assert 1.5.isFinite()
|
|
66
66
|
|
|
67
67
|
test "min/max compare across Int and Float without losing the original variant"
|
|
68
|
-
assert
|
|
68
|
+
assert 3.min(5).kind() == "Int"
|
|
69
|
-
assert
|
|
69
|
+
assert 3.max(2.5).kind() == "Int"
|
|
70
|
-
assert
|
|
70
|
+
assert 2.5.min(3).kind() == "Float"
|
|
71
71
|
|
|
72
72
|
test "toStr renders Int and Float correctly, including negatives and fractions"
|
|
73
|
-
assert
|
|
73
|
+
assert 42.toStr() == "42"
|
|
74
74
|
assert {-7}.toStr() == "-7"
|
|
75
|
-
assert
|
|
75
|
+
assert 0.toStr() == "0"
|
|
76
|
-
assert
|
|
76
|
+
assert 3.5.toStr() == "3.5"
|
|
77
77
|
assert {-3.5}.toStr() == "-3.5"
|
|
78
78
|
|
|
79
79
|
test "parseInt/parseFloat round-trip a rendered number"
|
|
@@ -84,13 +84,13 @@ test "parseInt/parseFloat round-trip a rendered number"
|
|
|
84
84
|
test "trig/hyperbolic/exp/ln free functions and Number methods still work post-migration"
|
|
85
85
|
assert sin(0.0f) == 0.0f
|
|
86
86
|
assert cos(0.0f) == 1.0f
|
|
87
|
-
assert
|
|
87
|
+
assert 0.sinh() == 0.0f
|
|
88
|
-
assert
|
|
88
|
+
assert 0.cosh() == 1.0f
|
|
89
89
|
|
|
90
90
|
test "string interpolation of a Float value works, via Number.toStr"
|
|
91
91
|
x := 3.5f
|
|
92
92
|
assert "value is {x}" == "value is 3.5"
|
|
93
|
-
y :=
|
|
93
|
+
y := 8.log2()
|
|
94
94
|
assert "computed is {y}" == "computed is 3.0"
|
|
95
95
|
|
|
96
96
|
test "assert of two directly-chained Float method calls renders a real failure message"
|
|
@@ -101,4 +101,4 @@ test "assert of two directly-chained Float method calls renders a real failure m
|
|
|
101
101
|
# chained method calls). Fixed at the root: `"{expr}"` interpolation of a
|
|
102
102
|
# Float now genuinely works (via `Number.toStr`), so the "Expected/Actual"
|
|
103
103
|
# failure-message machinery no longer needs to special-case Float at all.
|
|
104
|
-
assert
|
|
104
|
+
assert 8.log2() == 8.0.log2()
|