From 288d5750468064e0f998b30c27e266f3cc97a417 Mon Sep 17 00:00:00 2001 From: Stephen Chung Date: Wed, 11 Aug 2021 18:15:17 +0800 Subject: [PATCH] Add log10 for Decimal. --- CHANGELOG.md | 2 ++ Cargo.toml | 2 +- src/packages/math_basic.rs | 19 +++++++++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 013025aa..8b8e8aac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ Enhancements * Added a number of constants to `Dynamic`. * Added a number of constants and `fromXXX` constant methods to `Dynamic`. * `parse_float()`, `PI()` and `E()` now defer to `Decimal` under `no_float` if `decimal` is turned on. +* Added `log10()` for `Decimal`. +* `ln` for `Decimal` is now checked and won't panic. Version 1.0.2 diff --git a/Cargo.toml b/Cargo.toml index 701fdd60..cd52b2c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,7 +92,7 @@ default-features = false optional = true [dependencies.rust_decimal] -version = "1.14.2" +version = "1.15" default-features = false features = ["maths"] optional = true diff --git a/src/packages/math_basic.rs b/src/packages/math_basic.rs index 53c0431b..f3c9c5ea 100644 --- a/src/packages/math_basic.rs +++ b/src/packages/math_basic.rs @@ -339,8 +339,23 @@ mod decimal_functions { Ok(x.exp()) } } - pub fn ln(x: Decimal) -> Decimal { - x.ln() + #[rhai_fn(return_raw)] + pub fn ln(x: Decimal) -> Result> { + if cfg!(not(feature = "unchecked")) { + x.checked_ln() + .ok_or_else(|| make_err(format!("Error taking the natural log of {}", x))) + } else { + Ok(x.ln()) + } + } + #[rhai_fn(name = "log", return_raw)] + pub fn log10(x: Decimal) -> Result> { + if cfg!(not(feature = "unchecked")) { + x.checked_log10() + .ok_or_else(|| make_err(format!("Error taking the log of {}", x))) + } else { + Ok(x.log10()) + } } #[rhai_fn(name = "floor", get = "floor")] pub fn floor(x: Decimal) -> Decimal {