spec: rational numbers — 1/3 literals, arithmetic, numeric tower integration
SxRational type in OCaml (Rational of int * int, stored reduced, denom>0) and JS (SxRational class with _rational marker). n/d reader syntax in spec/parser.sx. Arithmetic contagion: int op rational → rational, rational op float → float. JS keeps int/int → float for CSS backward compatibility. OCaml as_number + safe_eq extended for cross-type rational equality so (= 2.5 5/2) → true. 62 tests in test-rationals.sx, all pass. JS: 2232 passed. OCaml: 4532 passed (+11 vs pre-fix baseline). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1034,4 +1034,30 @@
|
||||
:returns "any"
|
||||
:doc "Parse string s as a number. Optional radix (default 10). Returns nil on failure.")
|
||||
|
||||
(define-module :stdlib.rational)
|
||||
|
||||
(define-primitive
|
||||
"make-rational"
|
||||
:params (n d)
|
||||
:returns "rational"
|
||||
:doc "Rational n/d, auto-reduced by GCD. Error if d=0.")
|
||||
|
||||
(define-primitive
|
||||
"rational?"
|
||||
:params (v)
|
||||
:returns "boolean"
|
||||
:doc "True if v is a rational number.")
|
||||
|
||||
(define-primitive
|
||||
"numerator"
|
||||
:params ((r :as rational))
|
||||
:returns "integer"
|
||||
:doc "Numerator of rational r (after reduction).")
|
||||
|
||||
(define-primitive
|
||||
"denominator"
|
||||
:params ((r :as rational))
|
||||
:returns "integer"
|
||||
:doc "Denominator of rational r (after reduction, always positive).")
|
||||
|
||||
(define-module :stdlib.hash-table)
|
||||
|
||||
Reference in New Issue
Block a user