From 7dfb39d3db65693abae4dbcba5c185fe0cd1ea42 Mon Sep 17 00:00:00 2001 From: 2ez4gcx <108074836+2ez4gcx@users.noreply.github.com> Date: Fri, 27 Nov 2026 00:47:43 +0700 Subject: [PATCH] LISP: gcd and atoi give a real past 32 bits instead of wrapping `(gcd -2147483648 0)` gave 2147483647 (the divisor clamped to the largest integer), and `atoi` cast its digits with `as i32`, so "99999999999" gave 1215752191 and "99999999999999999999" gave 0. They now follow what the reader, `fix` and JSON inputs already do with a whole number that doesn't fit 32 bits: it is a real. `gcd` gives 2147483648.0 for its one such result; `atoi` gives the real `atof` reads from the digits, the number the reader makes of them. Results that fit are the same integers as before. Integer arithmetic still wraps. Part of #1933. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lisp/builtins/numbers.rs | 4 +++- crates/engine/src/lisp/builtins/strings.rs | 11 +++++++++-- crates/engine/src/lisp/tests.rs | 18 ++++++++++++++++++ docs/autolisp.md | 7 ++++--- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/lisp/builtins/numbers.rs b/crates/engine/src/lisp/builtins/numbers.rs index 2dfb6911c..642723f9a 100644 --- a/crates/engine/src/lisp/builtins/numbers.rs +++ b/crates/engine/src/lisp/builtins/numbers.rs @@ -205,6 +205,8 @@ pub(super) fn float(a: &[Value]) -> R { Ok(Value::Real(num(&a[0])?)) } +/// The greatest common divisor. The one past 32 bits, 2147483648 (of -2147483648 and 0 or +/// itself), is a real, as the reader and `fix` give such a number, not the largest integer. pub(super) fn gcd(a: &[Value]) -> R { arity(a, 2, 2)?; let (mut x, mut y) = (i64::from(int(&a[0])?).abs(), i64::from(int(&a[1])?).abs()); @@ -216,7 +218,7 @@ pub(super) fn gcd(a: &[Value]) -> R { x = y; y = t; } - Ok(Value::Int(i32::try_from(x).unwrap_or(i32::MAX))) + Ok(i32::try_from(x).map_or(Value::Real(x as f64), Value::Int)) } pub(super) fn logand(a: &[Value]) -> R { diff --git a/crates/engine/src/lisp/builtins/strings.rs b/crates/engine/src/lisp/builtins/strings.rs index 362183bd2..9a625f471 100644 --- a/crates/engine/src/lisp/builtins/strings.rs +++ b/crates/engine/src/lisp/builtins/strings.rs @@ -51,13 +51,20 @@ pub(super) fn itoa(a: &[Value]) -> R { Ok(Value::str(&int(&a[0])?.to_string())) } -/// C's `atoi`: leading blanks, a sign, digits; 0 when there are none. +/// C's `atoi`: leading blanks, a sign, digits; 0 when there are none. Digits past the 32-bit +/// range give a real, the number `atof` reads from them, as the reader gives one for such a +/// literal: neither wrapped nor clamped to an integer. pub(super) fn atoi(a: &[Value]) -> R { arity(a, 1, 1)?; let s = string(&a[0])?; let t = s.trim_start(); let end = t.char_indices().take_while(|(i, c)| c.is_ascii_digit() || (*i == 0 && (*c == '-' || *c == '+'))).count(); - Ok(Value::Int(t.get(..end).and_then(|n| n.parse::().ok()).map(|n| n as i32).unwrap_or(0))) + let digits = t.get(..end).unwrap_or_default(); + match digits.parse::() { + Ok(n) => Ok(Value::Int(n)), + Err(_) if digits.bytes().any(|b| b.is_ascii_digit()) => atof(&[Value::str(digits)]), + Err(_) => Ok(Value::Int(0)), + } } /// C's `atof`: the number token at the start; 0.0 when there is none. diff --git a/crates/engine/src/lisp/tests.rs b/crates/engine/src/lisp/tests.rs index 174e3ebdc..87a70fe15 100644 --- a/crates/engine/src/lisp/tests.rs +++ b/crates/engine/src/lisp/tests.rs @@ -1602,6 +1602,24 @@ fn a_lisp_value_at_a_prompt_is_not_evaluated_again() { assert_eq!(r, Some(4.0)); } +/// #1933: a whole number past 32 bits is a real, as the reader and `fix` make one, not a wrapped +/// or clamped integer: `atoi` gives the number its digits read as, `gcd` the true divisor. +#[test] +fn atoi_and_gcd_past_32_bits_give_reals() { + let mut s = session(); + for digits in ["2147483648", "-2147483649", "99999999999", "99999999999999999999"] { + assert_eq!(ok(&mut s, &format!("(= (atoi \"{digits}\") (read \"{digits}\"))")), "T", "{digits}"); + } + // Past every real too: the largest, as `atof` reads it. + let huge = "9".repeat(400); + assert_eq!(ok(&mut s, &format!("(= (atoi \"{huge}\") (atof \"{huge}\"))")), "T"); + // Within the range, integers as before. + assert_eq!(ok(&mut s, "(list (atoi \" -2147483648x\") (atoi \"+2147483647\") (atoi \"-\"))"), "(-2147483648 2147483647 0)"); + let past = "(abs (float -2147483648))"; + assert_eq!(ok(&mut s, &format!("(= (gcd -2147483648 0) (gcd 0 -2147483648) (gcd -2147483648 -2147483648) {past})")), "T"); + assert_eq!(ok(&mut s, "(list (gcd -2147483648 6) (gcd 2147483647 0))"), "(2 2147483647)"); +} + #[test] fn atof_out_of_range_exponent_reads_whole_token() { let mut s = Session::new(); diff --git a/docs/autolisp.md b/docs/autolisp.md index 2ed225ba8..70bfc7fdc 100644 --- a/docs/autolisp.md +++ b/docs/autolisp.md @@ -48,9 +48,10 @@ frame, so the window stays live and Esc stops it too. ## The language -Integers are 32-bit and wrap as AutoLISP's do; any real makes arithmetic real. Variables are -dynamically scoped: a function's arguments and its locals (after `/`) are bound while it runs. -Symbols are case-insensitive. Reals print with six significant digits. +Integers are 32-bit and wrap as AutoLISP's do; any real makes arithmetic real. A whole number +past that range read from text (source, `read`, `atoi`) or JSON, or made by `fix` or `gcd`, is a +real instead. Variables are dynamically scoped: a function's arguments and its locals (after `/`) +are bound while it runs. Symbols are case-insensitive. Reals print with six significant digits. Nothing a routine does can crash the program: deep recursion is the error "internal stack limit reached", malformed source is a reader error, and every built-in checks its arguments.