Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bug fixes
* `AST::clone_functions_only_filtered` now applies its filter, as does `AST::merge_filtered` when the first `AST` has no functions. Previously every function was kept (thanks [`@ImTheSquid`](https://github.com/ImTheSquid) [`#1192`](https://github.com/rhaiscript/rhai/pull/1192)).
* `sleep` no longer panics when called with a float number that is too large (thanks [`@Rockyrok77`](https://github.com/Rockyrok77) [`#1205`](https://github.com/rhaiscript/rhai/pull/1205)).
* Fixes string indexing bug with a single-character inclusive range (e.g. `s[1..=1]`) returning an empty string (thanks [`@youdie006`](https://github.com/youdie006) [`#1204`](https://github.com/rhaiscript/rhai/pull/1204)).
* `tan` no longer panics on a decimal number when the result overflows - e.g. `tan(π/2)` (thanks [`@Rockyrok77`](https://github.com/Rockyrok77) [`#1203`](https://github.com/rhaiscript/rhai/pull/1203)).

Enhancements
------------
Expand Down
6 changes: 4 additions & 2 deletions src/packages/math_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ mod decimal_functions {
x.cos()
}
/// Return the tangent of the decimal number in radians.
pub fn tan(x: Decimal) -> Decimal {
x.tan()
#[rhai_fn(return_raw)]
pub fn tan(x: Decimal) -> RhaiResultOf<Decimal> {
x.checked_tan()
.ok_or_else(|| make_err(format!("Error taking the tangent of {x}")))
}
/// Return the square root of the decimal number.
#[rhai_fn(return_raw)]
Expand Down
24 changes: 24 additions & 0 deletions tests/math.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rhai::{Engine, EvalAltResult, INT};

Check warning on line 1 in tests/math.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,sync,no_time,no_function,no_float,no_position,no...

unused import: `EvalAltResult`

Check warning on line 1 in tests/math.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,no_time,no_function,no_float,no_position,no_inde...

unused import: `EvalAltResult`

Check warning on line 1 in tests/math.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, --features testing-environ,unchecked,serde,metadata,internals,debugging,gra...

unused import: `EvalAltResult`

#[cfg(not(feature = "no_float"))]
use rhai::FLOAT;
Expand Down Expand Up @@ -57,3 +57,27 @@
assert_eq!(engine.eval::<INT>(r#"parse_int("42", 16)"#).unwrap(), 0x42);
assert_eq!(engine.eval::<INT>(r#"parse_int("abcdef", 16)"#).unwrap(), 0xabcdef);
}

#[cfg(feature = "decimal")]
#[test]
fn test_math_decimal_tan() {
use rust_decimal::Decimal;

let engine = Engine::new();

assert_eq!(engine.eval::<Decimal>(r#"parse_decimal("0").tan()"#).unwrap(), Decimal::ZERO);

// tan(π/2) and tan(3π/2) are infinite, which a Decimal cannot represent
assert!(matches!(
*engine
.eval::<Decimal>(r#"parse_decimal("1.5707963267948966192313216916").tan()"#)
.expect_err("expects tangent overflow"),
EvalAltResult::ErrorArithmetic(..)
));
assert!(matches!(
*engine
.eval::<Decimal>(r#"parse_decimal("4.7123889803846898576939650749").tan()"#)
.expect_err("expects tangent overflow"),
EvalAltResult::ErrorArithmetic(..)
));
}
Loading