diff --git a/CHANGELOG.md b/CHANGELOG.md index f89f789..f7b44a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## Next - Remove the `degrees` keyword which referred to `celcius` by default - Remove the `default_degrees` argument from `eval()` and `lex()`. Not necessary now that the `degrees` keyword is removed +- Fix trigonometry imprecision +- Fix unnecessary scientific notation ## 1.9.3 - 2023 sep 20 - Fix negative unary `-` always having higher precedence than `^`. This resulted in `-3^2` returning `9` instead of `-9` diff --git a/src/lib.rs b/src/lib.rs index b117b67..194ce80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,11 +65,18 @@ impl Number { pub const fn new(value: d128, unit: Unit) -> Number { Number { value, unit } } + pub fn get_simplified_value(&self) -> d128 { + // The order here matters, reduce must be first + // sin(pi) results in 0E-32, but .reduce() changes is to 0 + let mut value = self.value.reduce(); + // 0.2/0.01 results in 2E+1, but adding 0 changes it to 20 + value = value + d128!(0); + value + } } impl Display for Number { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - // 0.2/0.01 results in 2E+1, but if we add zero it becomes 20 - let value = self.value + d128!(0); + let value = self.get_simplified_value(); let word = match self.value == d128!(1) { true => self.unit.singular(), false => self.unit.plural(), @@ -290,6 +297,12 @@ mod tests { eval(input, true, false).unwrap() } + #[test] + fn test_simplify() { + assert_eq!(&default_eval("sin(pi)").to_string(), "0"); + assert_eq!(&default_eval("0.2/0.01").to_string(), "20"); + } + #[test] fn test_evaluations() { assert_eq!(default_eval("-2(-3)"), Number::new(d128!(6), Unit::NoUnit));