feat: allow changing max_digits for pretty_eval
This commit is contained in:
parent
69bf8f4635
commit
0afc7aab25
2 changed files with 13 additions and 15 deletions
|
|
@ -2,9 +2,7 @@ use rug::Float;
|
||||||
|
|
||||||
use crate::nodes::{self, Error};
|
use crate::nodes::{self, Error};
|
||||||
|
|
||||||
pub const MAX_DIGITS: usize = 10;
|
pub fn pretty_eval(mut input: String, max_digits: usize) -> Result<String, Error> {
|
||||||
|
|
||||||
pub fn pretty_eval(mut input: String) -> Result<String, Error> {
|
|
||||||
// no need to remove the characters here,
|
// no need to remove the characters here,
|
||||||
// as we ignore the them in all other logic already
|
// as we ignore the them in all other logic already
|
||||||
let should_add_back_alignment = input.contains('&');
|
let should_add_back_alignment = input.contains('&');
|
||||||
|
|
@ -21,7 +19,7 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
|
||||||
let result = nodes::eval(&input)?;
|
let result = nodes::eval(&input)?;
|
||||||
let display_digits =
|
let display_digits =
|
||||||
get_significant_digits(&result)
|
get_significant_digits(&result)
|
||||||
.min(MAX_DIGITS)
|
.min(max_digits)
|
||||||
.max(if result >= 1000 || result < 1 {
|
.max(if result >= 1000 || result < 1 {
|
||||||
1
|
1
|
||||||
} else if result >= 100 {
|
} else if result >= 100 {
|
||||||
|
|
@ -51,20 +49,20 @@ pub fn pretty_eval(mut input: String) -> Result<String, Error> {
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_character_readdition() {
|
fn test_character_readdition() {
|
||||||
assert_eq!(pretty_eval(" = 11".into()).unwrap(), " = 11");
|
assert_eq!(pretty_eval(" = 11".into(), 10).unwrap(), " = 11");
|
||||||
assert_eq!(pretty_eval(" &= 11".into()).unwrap(), " & = 11");
|
assert_eq!(pretty_eval(" &= 11".into(), 10).unwrap(), " & = 11");
|
||||||
assert_eq!(pretty_eval(" & 11".into()).unwrap(), " & 11");
|
assert_eq!(pretty_eval(" & 11".into(), 10).unwrap(), " & 11");
|
||||||
assert_eq!(pretty_eval(" & 11 \\".into()).unwrap(), " & 11 \\");
|
assert_eq!(pretty_eval(" & 11 \\".into(), 10).unwrap(), " & 11 \\");
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_basic_up_to_999() {
|
fn test_basic_up_to_999() {
|
||||||
assert_eq!(pretty_eval("1".into()).unwrap(), "1");
|
assert_eq!(pretty_eval("1".into(), 10).unwrap(), "1");
|
||||||
assert_eq!(pretty_eval("10".into()).unwrap(), "10");
|
assert_eq!(pretty_eval("10".into(), 10).unwrap(), "10");
|
||||||
assert_eq!(pretty_eval("100".into()).unwrap(), "100");
|
assert_eq!(pretty_eval("100".into(), 10).unwrap(), "100");
|
||||||
assert_eq!(pretty_eval("999".into()).unwrap(), "999");
|
assert_eq!(pretty_eval("999".into(), 10).unwrap(), "999");
|
||||||
assert_eq!(pretty_eval("1000".into()).unwrap(), "1 * 10^3");
|
assert_eq!(pretty_eval("1000".into(), 10).unwrap(), "1 * 10^3");
|
||||||
|
|
||||||
assert_eq!(pretty_eval("11.111111".into()).unwrap(), "11.111111");
|
assert_eq!(pretty_eval("11.111111".into(), 10).unwrap(), "11.111111");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_significant_digits(n: &Float) -> usize {
|
pub fn get_significant_digits(n: &Float) -> usize {
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ fn main() -> ExitCode {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match formatting::pretty_eval(input) {
|
match formatting::pretty_eval(input, 10) {
|
||||||
Ok(s) => println!("{s}"),
|
Ok(s) => println!("{s}"),
|
||||||
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue