aboutsummaryrefslogtreecommitdiffstats
path: root/core/test-module.c
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-10-19 01:13:07 +0100
committereunhhu <[email protected]>2026-10-19 01:13:07 +0100
commitb79f627af433a63f8f42869b124f5ef344a49352 (patch)
tree02aebc9cb63ace5040e5467949f3bec91edabf36 /core/test-module.c
parent7e1b3dc160906abaecfd4fe49ab2a51a3e2bfda4 (diff)
downloadpure-b79f627af433a63f8f42869b124f5ef344a49352.tar.gz
pure-b79f627af433a63f8f42869b124f5ef344a49352.zip
027's stage 5 designed and 5a made: two numbers (number, binary64, the default of literals and of counts, positions, lengths and u32 fields, whole to 2^53 exactly; i64 the exact integers past it, bits and money, never mixed, moved by number() and i64() when exact), / a number's division, // the floored quotient, i64 folds exact (only the result in range, so in any order), number folds in order unless the batch executor's 2^53 test says any order is exact, money as i64 minor units shown by .Nd, a replace carrying number, bool and i64 when exact; 5a: opcode FLOORDIV (95, 96 now): a number's floored quotient as Python's float //, exactly (a less its floored remainder, divided, to the nearest whole; a zero signed as a / b; by zero divide), a new opcode so the JIT, the tier and the batch executor leave it to the executor; unit test floor_division (7 // 2, -7 // 2, 7 // -2, 1 // 0.1 = 9, 0 // -3 = -0, 1e300 // 7, by zero); counts: b43 against b42 within 0.001% but BFS without batches -0.09%; the bank's queries (s42 and s43 in turns, six each) -0.20% to 0.00%; 008 and 027
Diffstat (limited to 'core/test-module.c')
-rw-r--r--core/test-module.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/core/test-module.c b/core/test-module.c
index cb8e228..36722d2 100644
--- a/core/test-module.c
+++ b/core/test-module.c
@@ -6962,6 +6962,33 @@ static void timers(void) {
CHECK(load(bad, &module_b) == PURE_INVALID && strstr(error, "view with an effect"));
}
+/* Floor division of numbers (027's stage 5): as Python's //, the quotient MOD's remainder leaves, exactly (1 // 0.1 is 9:
+ * 0.1 is a little more than a tenth), its zero signed as a / b; by zero it fails as divide. */
+static void floor_division(void) {
+ enum { FDIV = PURE_OP_FLOORDIV };
+ static const S nums[] = {{"a", 0}, {"b", 0}, {"c", 0}, {"d", 0}, {"e", 0}, {"f", 0}};
+ static const I inputs[] = {KEY("go", "KeyG"), KEY("zero", "KeyZ")};
+ static const O code[] = {
+ {C, 0, 7}, {C, 0, 2}, {FDIV, 0, 0}, {ST, 0, 0}, {C, 0, -7}, {C, 0, 2}, {FDIV, 0, 0}, {ST, 1, 0},
+ {C, 0, 7}, {C, 0, -2}, {FDIV, 0, 0}, {ST, 2, 0}, {C, 0, 1}, {C, 0, 0.1}, {FDIV, 0, 0}, {ST, 3, 0},
+ {C, 0, 0}, {C, 0, -3}, {FDIV, 0, 0}, {ST, 4, 0}, {C, 0, 1e300}, {C, 0, 7}, {FDIV, 0, 0}, {ST, 5, 0},
+ {C, 0, 1}, {C, 0, 0}, {FDIV, 0, 0}, {ST, 0, 0},
+ };
+ static const H handlers[] = {{1, 0, 24}, {3, 24, 4}};
+ static const V none[] = {{"v", 0, 1}};
+ Spec spec = SPEC(nums, inputs, none, handlers, code);
+ spec.nv = 0;
+ if (load(spec, &module_a) != PURE_OK) { fprintf(stderr, "floor division: %s\n", error); abort(); }
+ PureApp app;
+ CHECK(pure_app_start(&app, &module_a, (PureData){0}) == PURE_OK && press(&app, 0) == PURE_OK);
+ double huge = 1e300, quotient = (huge - fmod(huge, 7)) / 7;
+ CHECK(app.values[0].num == 3 && app.values[1].num == -4 && app.values[2].num == -4 && app.values[3].num == 9 &&
+ app.values[4].num == 0 && signbit(app.values[4].num) && app.values[5].num == floor(quotient));
+ CHECK(press(&app, 1) == PURE_INVALID && app.fault == PURE_FAULT_DIVIDE && app.values[0].num == 3);
+ pure_app_stop(&app);
+ printf("{\"floor_division\":6}\n");
+}
+
/* A number is written with the fewest digits that read back as it, the
* digits %g gives (with its exponent's leading zeros dropped): the same
* text as that search over precisions, for values of every kind. */
@@ -7312,6 +7339,7 @@ int main(void) {
bytes_fields();
anchors();
list_views();
+ floor_division();
memory_budget();
work_budget();
fold_mutations();