From d2efc74c568c731c4f59093826ad79531d10305a Mon Sep 17 00:00:00 2001 From: Daniel Rodgers-Pryor Date: Sun, 24 Dec 2017 10:55:32 +1100 Subject: [PATCH] Fix exp function to actually work It was calling into decNumberExp with the wrong signature, so it didn't work at all. I also fixed the decNumber tests for exp so that they'll actually run if point at the exp.decTest file, and I added a super basic test for ln and exp. It seems a bit too easy to not run some of the decNumber test by accident right now; maybe it would be good to expect every test file to have some non-igored tests, and then just delete the test files for actually unimplemented functionality? --- decTest/exp.decTest | 1 + src/bin/run-test.rs | 1 + src/dec128.rs | 12 +++++++----- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/decTest/exp.decTest b/decTest/exp.decTest index 8385dcde..74fdbcaf 100644 --- a/decTest/exp.decTest +++ b/decTest/exp.decTest @@ -540,6 +540,7 @@ expx1200 exp 1 -> 2.718281828459045235360287471352662 Inexact Rounded Precision: 34 maxExponent: 6144 minExponent: -6143 +clamp: 1 expx1201 exp 309.5948855821510212996700645087188 -> 2.853319692901387521201738015050724E+134 Inexact Rounded expx1202 exp 9.936543068706211420422803962680164 -> 20672.15839203171877476511093276022 Inexact Rounded expx1203 exp 6.307870323881505684429839491707908 -> 548.8747777054637296137277391754665 Inexact Rounded diff --git a/src/bin/run-test.rs b/src/bin/run-test.rs index 72e0bba0..f9a07219 100644 --- a/src/bin/run-test.rs +++ b/src/bin/run-test.rs @@ -494,6 +494,7 @@ fn run_test<'a>(env: &Environment, test: Test<'a>) -> TestResult<'a> { &test); } Op::Divide(a, b) => simple_op!(test, value = div(a, b)), + Op::Exp(a) => simple_op!(test, value = exp(a)), Op::Fma(a, b, c) => simple_op!(test, value = mul_add(a, b, c)), Op::Invert(a) => simple_op!(test, value = not(a)), Op::LogB(a) => simple_op!(test, value = logb(a)), diff --git a/src/dec128.rs b/src/dec128.rs index 6841640a..bec2e1af 100644 --- a/src/dec128.rs +++ b/src/dec128.rs @@ -617,13 +617,11 @@ impl d128 { /// respectively). Inexact results will almost always be correctly rounded, but may be up to 1 /// ulp (unit in last place) in error in rare cases. This is a mathematical function; the /// 106 restrictions on precision and range apply as described above. - pub fn exp>(mut self, exp: O) -> d128 { + pub fn exp(mut self) -> d128 { d128::with_context(|ctx| unsafe { let mut num_self: decNumber = uninitialized(); - let mut num_exp: decNumber = uninitialized(); decimal128ToNumber(&self, &mut num_self); - decimal128ToNumber(exp.as_ref(), &mut num_exp); - decNumberExp(&mut num_self, &num_self, &num_exp, ctx); + decNumberExp(&mut num_self, &num_self, ctx); *decimal128FromNumber(&mut self, &num_self, ctx) }) } @@ -948,7 +946,6 @@ extern "C" { ctx: *mut Context) -> *mut decNumber; fn decNumberExp(res: *mut decNumber, - lhs: *const decNumber, rhs: *const decNumber, ctx: *mut Context) -> *mut decNumber; @@ -1093,4 +1090,9 @@ mod tests { assert_eq!(d128::from_str(&(::std::u64::MIN).to_string()).unwrap(), d128::from(::std::u64::MIN)); } + + #[test] + fn math_op() { + assert_eq!(d128!(1), d128!(1).ln().exp()); + } }