Compare commits
2 changed files with 5 additions and 28 deletions
|
|
@ -19,22 +19,10 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
|
||||||
.is_some();
|
.is_some();
|
||||||
|
|
||||||
let result = nodes::eval(&input)?;
|
let result = nodes::eval(&input)?;
|
||||||
let display_digits =
|
let significant_digits = get_significant_digits(&result).min(MAX_DIGITS);
|
||||||
get_significant_digits(&result)
|
|
||||||
.min(MAX_DIGITS)
|
|
||||||
.max(if result >= 1000 || result < 1 {
|
|
||||||
1
|
|
||||||
} else if result >= 100 {
|
|
||||||
3
|
|
||||||
} else if result >= 10 {
|
|
||||||
2
|
|
||||||
// if result >= 1
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(format!(
|
Ok(format!(
|
||||||
"{}{}{result:.display_digits$}{}",
|
"{}{}{result:.significant_digits$}{}",
|
||||||
" ".repeat(leading_spaces),
|
" ".repeat(leading_spaces),
|
||||||
if should_add_back_equals && should_add_back_alignment {
|
if should_add_back_equals && should_add_back_alignment {
|
||||||
"& = "
|
"& = "
|
||||||
|
|
@ -46,8 +34,7 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
|
||||||
""
|
""
|
||||||
},
|
},
|
||||||
if should_add_back_backslash { " \\" } else { "" }
|
if should_add_back_backslash { " \\" } else { "" }
|
||||||
)
|
))
|
||||||
.replace('e', " * 10^"))
|
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_character_readdition() {
|
fn test_character_readdition() {
|
||||||
|
|
@ -56,19 +43,9 @@ fn test_character_readdition() {
|
||||||
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11");
|
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11");
|
||||||
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\");
|
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\");
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
fn test_basic_up_to_999() {
|
|
||||||
assert_eq!(pretty_eval("1".into()).unwrap(), "1");
|
|
||||||
assert_eq!(pretty_eval("10".into()).unwrap(), "10");
|
|
||||||
assert_eq!(pretty_eval("100".into()).unwrap(), "100");
|
|
||||||
assert_eq!(pretty_eval("999".into()).unwrap(), "999");
|
|
||||||
assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3");
|
|
||||||
|
|
||||||
assert_eq!(pretty_eval("11.111111".into()).unwrap(), "11.111111");
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_significant_digits(n: &Float) -> usize {
|
pub fn get_significant_digits(n: &Float) -> usize {
|
||||||
let raw_string = n.to_string().replace('.', "");
|
let raw_string: String = format!("{n}").chars().filter(|c| *c != '.').collect();
|
||||||
|
|
||||||
let mut consecutive_zeros = 0;
|
let mut consecutive_zeros = 0;
|
||||||
let mut consecutive_nines = 0;
|
let mut consecutive_nines = 0;
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ pub fn eval(s: &str) -> Result<rug::Float, Error> {
|
||||||
Ok(Nodes::from_str(s)?.evaluate()?)
|
Ok(Nodes::from_str(s)?.evaluate()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const PREC: u32 = 2048;
|
pub const PREC: u32 = 512;
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone, PartialEq)]
|
#[derive(Debug, Default, Clone, PartialEq)]
|
||||||
pub struct Nodes(Vec<Node>);
|
pub struct Nodes(Vec<Node>);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue