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 <noreply@anthropic.com>
This commit is contained in:
2ez4gcxandClaude Opus 5.5 committed 2026-11-27 00:47:43 +07:00
1 parent a3ed0682ab
commit 7dfb39d3db
4 files changed
+34 -6

No files matched your search

+3 -1
View File
@@ -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 {
+9 -2
View File
@@ -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::<i64>().ok()).map(|n| n as i32).unwrap_or(0)))
let digits = t.get(..end).unwrap_or_default();
match digits.parse::<i32>() {
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.
+18
View File
@@ -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();
+4 -3
View File
@@ -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.