Avoid unnecessary scientific notation
This commit is contained in:
parent
0a1a108c0b
commit
8cfac98bc7
@ -3,6 +3,8 @@
|
|||||||
## Next
|
## Next
|
||||||
- Remove the `degrees` keyword which referred to `celcius` by default
|
- 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
|
- 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
|
## 1.9.3 - 2023 sep 20
|
||||||
- Fix negative unary `-` always having higher precedence than `^`. This resulted in `-3^2` returning `9` instead of `-9`
|
- Fix negative unary `-` always having higher precedence than `^`. This resulted in `-3^2` returning `9` instead of `-9`
|
||||||
|
|||||||
17
src/lib.rs
17
src/lib.rs
@ -65,11 +65,18 @@ impl Number {
|
|||||||
pub const fn new(value: d128, unit: Unit) -> Number {
|
pub const fn new(value: d128, unit: Unit) -> Number {
|
||||||
Number { value, unit }
|
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 {
|
impl Display for Number {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
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.get_simplified_value();
|
||||||
let value = self.value + d128!(0);
|
|
||||||
let word = match self.value == d128!(1) {
|
let word = match self.value == d128!(1) {
|
||||||
true => self.unit.singular(),
|
true => self.unit.singular(),
|
||||||
false => self.unit.plural(),
|
false => self.unit.plural(),
|
||||||
@ -290,6 +297,12 @@ mod tests {
|
|||||||
eval(input, true, false).unwrap()
|
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]
|
#[test]
|
||||||
fn test_evaluations() {
|
fn test_evaluations() {
|
||||||
assert_eq!(default_eval("-2(-3)"), Number::new(d128!(6), Unit::NoUnit));
|
assert_eq!(default_eval("-2(-3)"), Number::new(d128!(6), Unit::NoUnit));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user