Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes e^-1 incorrectly returning early before calculation had occured #339

Merged
merged 2 commits into from
Apr 16, 2021
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
20 changes: 11 additions & 9 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const MAX: Decimal = Decimal {
hi: 4_294_967_295,
};

pub(crate) const ONE: Decimal = Decimal {
flags: 0,
lo: 1,
mid: 0,
hi: 0,
};

// Fast access for 10^n where n is 0-9
const POWERS_10: [u32; 10] = [
1,
Expand Down Expand Up @@ -606,7 +613,7 @@ impl Decimal {
// Opportunity for optimization here
let floored = self.trunc();
if self.is_sign_negative() && !self.fract().is_zero() {
floored - Decimal::one()
floored - ONE
} else {
floored
}
Expand All @@ -633,7 +640,7 @@ impl Decimal {

// Opportunity for optimization here
if self.is_sign_positive() && !self.fract().is_zero() {
self.trunc() + Decimal::one()
self.trunc() + ONE
} else {
self.trunc()
}
Expand Down Expand Up @@ -3068,12 +3075,7 @@ impl Zero for Decimal {

impl One for Decimal {
fn one() -> Decimal {
Decimal {
flags: 0,
hi: 0,
lo: 1,
mid: 0,
}
ONE
}
}

Expand All @@ -3094,7 +3096,7 @@ impl Signed for Decimal {
if self.is_zero() {
Decimal::zero()
} else {
let mut value = Decimal::one();
let mut value = ONE;
if self.is_sign_negative() {
value.set_sign_negative(true);
}
Expand Down
14 changes: 7 additions & 7 deletions src/maths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use crate::prelude::*;
const TWO: Decimal = Decimal::from_parts_raw(2, 0, 0, 0);
const PI: Decimal = Decimal::from_parts_raw(1102470953, 185874565, 1703060790, 1835008);
const LN2: Decimal = Decimal::from_parts_raw(2831677809, 328455696, 3757558395, 1900544);
const EXP_TOLERANCE: Decimal = Decimal::from_parts(2, 0, 0, false, 7);

pub trait MathematicalOps {
/// The estimated exponential function, e<sup>x</sup>, rounded to 8 decimal places. Stops
/// calculating when it is within tolerance is roughly 0.000002 in order to prevent
/// calculating when it is within tolerance of roughly 0.000002 in order to prevent
/// multiplication overflow.
fn exp(&self) -> Decimal;

Expand Down Expand Up @@ -43,11 +44,10 @@ pub trait MathematicalOps {

impl MathematicalOps for Decimal {
/// The estimated exponential function, e<sup>x</sup>, rounded to 8 decimal places. Stops
/// calculating when it is within tolerance is roughly 0.000002 in order to prevent
/// calculating when it is within tolerance of roughly 0.000002 in order to prevent
/// multiplication overflow.
fn exp(&self) -> Decimal {
let tolerance = Decimal::new(2, 7);
self.exp_with_tolerance(tolerance)
self.exp_with_tolerance(EXP_TOLERANCE)
}

/// The estimated exponential function, e<sup>x</sup>, rounded to 8 decimal places. Stops
Expand All @@ -63,14 +63,14 @@ impl MathematicalOps for Decimal {

let mut term = *self;
let mut result = self + Decimal::one();
let mut prev_result = Decimal::zero();
let mut prev_result: Option<Decimal> = None;
let mut factorial = Decimal::one();
let mut n = TWO;
let twenty_four = Decimal::new(24, 0);

// Needs rounding because multiplication overflows otherwise.
while (result - prev_result).abs() > tolerance && n < twenty_four {
prev_result = result;
while (prev_result.is_none() || (result - prev_result.unwrap()).abs() > tolerance) && n < twenty_four {
prev_result = Some(result);
term = self * term.round_dp(8);
factorial *= n;
result += (term / factorial).round_dp(8);
Expand Down
27 changes: 11 additions & 16 deletions tests/decimal_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,24 +1980,19 @@ mod maths {
#[test]
fn test_exp() {
let test_cases = &[
(Decimal::new(10, 0), Decimal::from_str("22023.81992829").unwrap()),
(Decimal::new(11, 0), Decimal::from_str("59846.36875797").unwrap()),
(Decimal::new(3, 0), Decimal::from_str("20.08553690").unwrap()),
(
Decimal::from_str("8").unwrap(),
Decimal::from_str("2980.94688158").unwrap(),
),
(
Decimal::from_str("0.1").unwrap(),
Decimal::from_str("1.10517092").unwrap(),
),
(
Decimal::from_str("2.0").unwrap(),
Decimal::from_str("7.38905609").unwrap(),
),
("10", "22023.81992829"),
("11", "59846.36875797"),
("3", "20.08553690"),
("8", "2980.94688158"),
("0.1", "1.10517092"),
("2.0", "7.38905609"),
("-2", "0.13533531"),
("-1", "0.36787944"),
];
for case in test_cases {
assert_eq!(case.1, case.0.exp());
let x = Decimal::from_str(case.0).unwrap();
let expected = Decimal::from_str(case.1).unwrap();
assert_eq!(expected, x.exp());
}
}

Expand Down