mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-26 20:52:29 +02:00
Improved output.
This commit is contained in:
parent
60384ca8a8
commit
5b9e9849e8
@ -4,28 +4,36 @@ use kalk::parser::{self};
|
|||||||
pub fn eval(parser: &mut parser::Context, input: &str) {
|
pub fn eval(parser: &mut parser::Context, input: &str) {
|
||||||
match parser::parse(parser, input, 53) {
|
match parser::parse(parser, input, 53) {
|
||||||
Ok(Some(result)) => {
|
Ok(Some(result)) => {
|
||||||
|
let (_, digits, exp_option) = result.to_sign_string_exp(10, None);
|
||||||
|
let exp = if let Some(exp) = exp_option { exp } else { 0 };
|
||||||
|
|
||||||
if result.is_infinite() {
|
if result.is_infinite() {
|
||||||
err("Too big to process.");
|
err("Too big to process.");
|
||||||
} else if result > 100_000_000 || result < -100_000_00 {
|
/*} else if result.clone().fract() == 0 {
|
||||||
let sign = if result >= 0 { "" } else { "-" };
|
println!("{}", result.to_integer().unwrap());*/
|
||||||
let (_, digits, exp) = result.to_sign_string_exp(10, None);
|
|
||||||
let value = format!(
|
|
||||||
"{}.{}",
|
|
||||||
digits[0..1].to_string(),
|
|
||||||
digits[1..].to_string().trim_end_matches('0')
|
|
||||||
);
|
|
||||||
|
|
||||||
println!("{}{}*10^{}", sign, value, exp.unwrap() - 1);
|
|
||||||
} else if result.clone().fract() == 0 {
|
|
||||||
println!("{}", result.to_integer().unwrap());
|
|
||||||
} else {
|
} else {
|
||||||
println!(
|
let use_sci_notation = exp > 8 || exp < -6;
|
||||||
"{}",
|
let comma_pos = if use_sci_notation { 1 } else { exp as usize };
|
||||||
result
|
let sign = if result >= 0 { "" } else { "-" };
|
||||||
.to_string()
|
|
||||||
.trim_end_matches('0')
|
let num = if use_sci_notation {
|
||||||
.trim_end_matches('.')
|
// Insert the comma if there are supposed to be decimals.
|
||||||
);
|
let mut chars: Vec<char> = digits.trim_end_matches('0').chars().collect();
|
||||||
|
chars.insert(comma_pos, '.');
|
||||||
|
chars.into_iter().collect::<String>()
|
||||||
|
} else if exp < 0 {
|
||||||
|
// 0 < x < 1
|
||||||
|
format!("0.{}{}", "0".repeat(exp.abs() as usize), digits)
|
||||||
|
} else {
|
||||||
|
// Regular number
|
||||||
|
digits[..(exp as usize)].to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
if use_sci_notation {
|
||||||
|
println!("{}{}*10^{}", sign, num, exp);
|
||||||
|
} else {
|
||||||
|
println!("{}{}", sign, num);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(None) => print!(""),
|
Ok(None) => print!(""),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user