Skip to content

fix: raise BASIC exceptions for math domain errors - #1

Closed
3583Bytes wants to merge 1 commit into
mainfrom
fix/math-domain-errors
Closed

3583Bytes wants to merge 1 commit into
mainfrom
fix/math-domain-errors

Conversation

@3583Bytes

Copy link
Copy Markdown
Owner

Problem

PRINT SQR(-1) printed runtime error: Input string was not in a correct format. Math builtins pass their double result through FromDouble, which parses d.ToString("R") into a BigDecimal. A NaN or infinite result (SQR of a negative, ASIN/ACOS outside [-1, 1], EXP overflow, SEC/CSC/COT at their poles) made that parse throw a .NET FormatException, so the error couldn't be handled with WHEN EXCEPTION and the message was meaningless.

Found while running Arcade BASIC in the browser on adamberent.com.

Fix

  • SQR rejects negative arguments with exception 2001 ("SQR requires non-negative argument"), the same code LOG uses for its domain error.
  • FromDouble takes the function name and reports NaN as 2001 ("ASIN argument out of range") and infinity as 1001 ("EXP result overflows"), the code already used for division by zero.

Both engines share BuiltinImpls, so the tree-walker and the VM behave the same.

Tests

Regression theory added to both ExceptionTests (interpreter) and VmTests: SQR(-1), ASIN(2), ACOS(-2) and EXP(1000) are caught by WHEN EXCEPTION with the expected EXTYPE and EXTEXT$. Full suite: 574 passed; Release build has 0 warnings.

🤖 Generated with Claude Code

SQR of a negative number (and ASIN/ACOS out of range, EXP overflow,
SEC/CSC/COT at their poles) produced NaN or infinity, which FromDouble
then failed to parse. The .NET FormatException escaped as "runtime error:
Input string was not in a correct format." and could not be handled with
WHEN EXCEPTION.

SQR now rejects negative arguments with exception 2001, matching LOG, and
FromDouble reports NaN as 2001 and infinity as 1001, naming the function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@3583Bytes 3583Bytes closed this Oct 15, 2026
@3583Bytes
3583Bytes deleted the fix/math-domain-errors branch October 15, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants