diff --git a/Cargo.toml b/Cargo.toml index dd2d32de..f2236a0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,7 +92,7 @@ default_features = false optional = true [dependencies.rust_decimal] -version = "1.13" +version = "1.14" default_features = false features = ["maths"] optional = true diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 130c36a5..1e790000 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -317,13 +317,8 @@ mod decimal_functions { #[rhai_fn(return_raw)] pub fn exp(x: Decimal) -> Result> { if cfg!(not(feature = "unchecked")) { - if x > Decimal::from_parts(117578, 0, 0, false, 4) { - Err(make_err(format!("Exponential overflow: e ** {}", x,))) - } else if x < Decimal::from_parts(8, 0, 0, true, 0) { - Err(make_err(format!("Exponential underflow: e ** {}", x,))) - } else { - Ok(x.exp()) - } + x.checked_exp() + .ok_or_else(|| make_err(format!("Exponential overflow: e ** {}", x,))) } else { Ok(x.exp()) } @@ -346,15 +341,15 @@ mod decimal_functions { #[rhai_fn(name = "round", return_raw)] pub fn round_dp(x: Decimal, dp: INT) -> Result> { if cfg!(not(feature = "unchecked")) { - if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { - return Ok(x); - } if dp < 0 { return Err(make_err(format!( - "Decimal value {} round to a negative index: {}", - x, dp + "Invalid number of digits for rounding: {}", + dp ))); } + if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { + return Ok(x); + } } Ok(x.round_dp(dp as u32)) @@ -362,15 +357,15 @@ mod decimal_functions { #[rhai_fn(return_raw)] pub fn round_up(x: Decimal, dp: INT) -> Result> { if cfg!(not(feature = "unchecked")) { - if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { - return Ok(x); - } if dp < 0 { return Err(make_err(format!( - "Decimal value {} round to a negative index: {}", - x, dp + "Invalid number of digits for rounding: {}", + dp ))); } + if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { + return Ok(x); + } } Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::AwayFromZero)) @@ -378,15 +373,15 @@ mod decimal_functions { #[rhai_fn(return_raw)] pub fn round_down(x: Decimal, dp: INT) -> Result> { if cfg!(not(feature = "unchecked")) { - if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { - return Ok(x); - } if dp < 0 { return Err(make_err(format!( - "Decimal value {} round to a negative index: {}", - x, dp + "Invalid number of digits for rounding: {}", + dp ))); } + if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { + return Ok(x); + } } Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::ToZero)) @@ -394,15 +389,15 @@ mod decimal_functions { #[rhai_fn(return_raw)] pub fn round_half_up(x: Decimal, dp: INT) -> Result> { if cfg!(not(feature = "unchecked")) { - if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { - return Ok(x); - } if dp < 0 { return Err(make_err(format!( - "Decimal value {} round to a negative index: {}", - x, dp + "Invalid number of digits for rounding: {}", + dp ))); } + if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { + return Ok(x); + } } Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::MidpointAwayFromZero)) @@ -410,15 +405,15 @@ mod decimal_functions { #[rhai_fn(return_raw)] pub fn round_half_down(x: Decimal, dp: INT) -> Result> { if cfg!(not(feature = "unchecked")) { - if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { - return Ok(x); - } if dp < 0 { return Err(make_err(format!( - "Decimal value {} round to a negative index: {}", - x, dp + "Invalid number of digits for rounding: {}", + dp ))); } + if cfg!(not(feature = "only_i32")) && dp > (u32::MAX as INT) { + return Ok(x); + } } Ok(x.round_dp_with_strategy(dp as u32, RoundingStrategy::MidpointTowardZero))