Widen pseudo for numerical work: sized types, FLOAT32, exact built-ins, USE IEEE
Pseudo could say what a procedure computes only up to INTEGER and FLOAT, so it could not state what a Mojo or Python program with Int32, UInt8 or Float32 values will compute, and it had no words for rounding itself. - lang/parse: INT8..INT64, UINT8..UINT64, FLOAT32, FLOAT64 as types (INTEGER or FLOAT with a width, so every "whole number" rule accepts them); conversions written as Mojo writes them, INT32(X), FLOAT32(X); 1_000_000, 0xFF and 0b1010 literals; a type name as an argument (MAX_VALUE(INT8)); the USE line (USE IEEE now, library modules next). - check: widths and precisions never mix silently (mixed-width, mixed-precision); a literal takes the type of the sized value beside it, as in Mojo, and one that does not fit is an error before any run (literal-range); typing for every new built-in. - interp: a sized result, store, parameter, RETURN or conversion that does not fit stops the run (kind overflow), WRAP(X, T) wraps on purpose; FLOAT32 results are rounded to single precision; USE IEEE lets NaN and infinities flow. - numeric.js: exact helpers: FMA rounded once, EXACT(X) with every digit a FLOAT holds, ULP and NEXT_AFTER for both precisions, bit operations in two's complement. - Built-ins: ASIN ACOS ATAN SINH COSH TANH HYPOT POW CBRT EXPM1 LOG1P E TRUNC SIGN COPYSIGN CLAMP FMA IS_NAN IS_INF IS_FINITE INF NAN ULP NEXT_AFTER IS_CLOSE EXACT MAX_VALUE MIN_VALUE EPSILON GCD LCM BIT_AND BIT_OR BIT_XOR BIT_NOT SHIFT_LEFT SHIFT_RIGHT WRAP. They stay usable as names (a variable E, a FUNCTION GCD): the program's own wins. - vcgen: an overflow obligation on every sized result, then the range as a fact (also for sized parameters and loop-cut variables); FLOAT32 arithmetic as its own uninterpreted functions, constants rounded exactly, TOFLOAT32(v) otherwise; argument obligations for SQRT, the logarithms, ASIN/ACOS, LOG1P, CLAMP and the shifts; nothing proved under USE IEEE. - testing: the fuzzer tries the edges of a sized type and generates FLOAT32 values. - pseudo-to-mojo: Int8..Float32, conversions, the new built-ins through std.math (each name in the harvested index), a note where Mojo wraps. - Tests: numeric.test.mjs; corpus 49-52; prover fixtures int8-overflow, float32-store, ieee-nan; the reference documents every new form. Gate: 324 pass (core). New Mojo uses std.math names the interpreter has not been verified on (listed in sync-mojo's KNOWN_GAPS); they need pixi run mojo-verify on the network before the interpreter runs them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
24 files changed
+1271
-96
No files matched your search
@@ -0,0 +1,14 @@
|
||||
// Sized whole numbers: arithmetic that stays inside its type, wrapping only when asked, and conversions that check.
|
||||
// EXPECT: 255 0 255
|
||||
// EXPECT: 1500000000 -2 256
|
||||
// EXPECT: 8 14 6 255 1024 -4
|
||||
// EXPECT: 12 12 -128 2147483647
|
||||
DECLARE Count : UINT8 <- 250
|
||||
Count <- Count + 5
|
||||
OUTPUT Count, " ", WRAP(Count + 1, UINT8), " ", MAX_VALUE(UINT8)
|
||||
DECLARE Big : INT64 <- 3_000_000_000
|
||||
DECLARE Small : INT32
|
||||
Small <- INT32(Big DIV 2)
|
||||
OUTPUT Small, " ", INT16(-2.9), " ", 0xFF + 0b1
|
||||
OUTPUT BIT_AND(0b1100, 0b1010), " ", BIT_OR(12, 10), " ", BIT_XOR(12, 10), " ", BIT_NOT(UINT8(0)), " ", SHIFT_LEFT(1, 10), " ", SHIFT_RIGHT(-8, 1)
|
||||
OUTPUT GCD(84, 36), " ", LCM(4, 6), " ", MIN_VALUE(INT8), " ", MAX_VALUE(INT32)
|
||||
@@ -0,0 +1,20 @@
|
||||
// Single precision: every FLOAT32 result is rounded to 24 bits, and a literal next to a FLOAT32 is a FLOAT32.
|
||||
// EXPECT: 1.0000001 0.9999999999999999
|
||||
// EXPECT: TRUE FALSE
|
||||
// EXPECT: 0.100000001490116119384765625
|
||||
// EXPECT: 16777216.0 1.1920929e-7 1.1920929e-7
|
||||
// EXPECT: 5.551115123125783e-17 0.0
|
||||
// EXPECT: TRUE TRUE
|
||||
DECLARE X : FLOAT32 <- 0.1
|
||||
DECLARE Total32 : FLOAT32 <- 0.0
|
||||
DECLARE Total64 : FLOAT <- 0.0
|
||||
FOR I <- 0 TO 10
|
||||
Total32 <- Total32 + X
|
||||
Total64 <- Total64 + 0.1
|
||||
NEXT I
|
||||
OUTPUT Total32, " ", Total64
|
||||
OUTPUT X == 0.1, " ", FLOAT64(X) == 0.1
|
||||
OUTPUT EXACT(X)
|
||||
OUTPUT FLOAT32(16777217), " ", ULP(FLOAT32(1.0)), " ", EPSILON(FLOAT32)
|
||||
OUTPUT FMA(0.1, 10.0, -1.0), " ", 0.1 * 10.0 - 1.0
|
||||
OUTPUT IS_CLOSE(Total32, 1.0, 1e-6, 0.0), " ", IS_CLOSE(Total64, 1.0, 1e-15, 0.0)
|
||||
@@ -0,0 +1,12 @@
|
||||
// USE IEEE: NaN and the infinities flow through a calculation as IEEE 754 says, instead of stopping the run.
|
||||
// EXPECT: infinity -infinity not a number
|
||||
// EXPECT: TRUE TRUE FALSE TRUE
|
||||
// EXPECT: not a number -infinity FALSE
|
||||
USE IEEE
|
||||
DECLARE Zero : FLOAT <- 0.0
|
||||
DECLARE Inf : FLOAT
|
||||
Inf <- 1.0 / Zero
|
||||
DECLARE NotANumber : FLOAT <- Inf - Inf
|
||||
OUTPUT Inf, " ", -Inf, " ", NotANumber
|
||||
OUTPUT IS_INF(Inf), " ", IS_NAN(NotANumber), " ", NotANumber == NotANumber, " ", NotANumber != NotANumber
|
||||
OUTPUT SQRT(-1.0), " ", LOG(0.0), " ", IS_FINITE(MAX_VALUE(FLOAT64) * 2.0)
|
||||
@@ -0,0 +1,11 @@
|
||||
// Elementary functions for numerical work, and the accurate forms near 0.
|
||||
// EXPECT: 5.0 TRUE -3.0 1024.0
|
||||
// EXPECT: 1.00000000005e-10 1.000000082740371e-10 9.999999999500001e-11
|
||||
// EXPECT: -2.0 -1 -2.0 10 0.0
|
||||
// EXPECT: 3.142 3.142 0.0 0.0 2.718
|
||||
// EXPECT: TRUE 2.220446049250313e-16
|
||||
OUTPUT HYPOT(3.0, 4.0), " ", HYPOT(1e200, 1e200) > 1e200, " ", CBRT(-27.0), " ", POW(2.0, 10.0)
|
||||
OUTPUT EXPM1(1e-10), " ", EXP(1e-10) - 1.0, " ", LOG1P(1e-10)
|
||||
OUTPUT TRUNC(-2.7), " ", SIGN(-3), " ", COPYSIGN(2.0, -0.0), " ", CLAMP(15, 0, 10), " ", CLAMP(-0.5, 0.0, 1.0)
|
||||
OUTPUT ROUND(ASIN(1.0) * 2 * 1000) / 1000, " ", ROUND(ACOS(-1.0) * 1000) / 1000, " ", ATAN(0.0), " ", TANH(0.0), " ", ROUND(E() * 1000) / 1000
|
||||
OUTPUT NEXT_AFTER(1.0, 2.0) - 1.0 == EPSILON(FLOAT64), " ", ULP(1.0)
|
||||
Reference in new issue
Block a user