Skip to content

Do not panic when tan of a decimal number overflows - #1203

Merged
schungx merged 2 commits into
rhaiscript:mainfrom
Rockyrok77:decimal-tan-checked
Oct 29, 2026
Merged

schungx merged 2 commits into
rhaiscript:mainfrom
Rockyrok77:decimal-tan-checked

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

With the decimal feature, tan on a decimal number panics when the result overflows:

parse_decimal("1.5707963267948966192313216916").tan();   // π/2: panics with "Tan overflowed"

decimal_functions::tan calls rust_decimal's MathematicalOps::tan, which panics whenever checked_tan returns None. That happens at π/2 (and 3π/2), where the reciprocal it takes divides by zero. The panic comes from inside a native function, so no engine limit stops it, and a script can take down the host with one call. sqrt, exp, ln and log10 in the same module already use the checked forms and raise ErrorArithmetic; tan now does the same. sin and cos were checked too: their checked_ forms cannot return None for any Decimal, so they are left alone.

Added test_math_decimal_tan to tests/math.rs. Without the fix it panics; with it, it passes. cargo test --tests passes with default features and with the decimal, no_float,decimal, decimal,unchecked, only_i32,decimal and decimal,serde,metadata,internals,debugging,grain rows of the CI matrix; cargo fmt --check and the CI clippy invocation are clean. CHANGELOG line under 1.27.1's bug fixes.

@schungx

schungx commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

There are merge conflicts. Can you fix? Thanks!

@schungx
schungx merged commit e4694f3 into rhaiscript:main Oct 29, 2026
55 checks passed
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