diff --git a/kalk_cli/Cargo.lock b/kalk_cli/Cargo.lock index 2c6277b..8c8d8f8 100644 --- a/kalk_cli/Cargo.lock +++ b/kalk_cli/Cargo.lock @@ -134,6 +134,7 @@ version = "0.1.0" dependencies = [ "phf", "rug", + "test-case", ] [[package]] @@ -392,6 +393,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "test-case" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "199464148b42bcf3da8b2a56f6ee87ca68f47402496d1268849291ec9fb463c8" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "version_check", +] + [[package]] name = "unicode-segmentation" version = "1.6.0" @@ -416,6 +429,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + [[package]] name = "void" version = "1.0.2" diff --git a/kalk_cli/src/output.rs b/kalk_cli/src/output.rs index f53ed8e..d080061 100644 --- a/kalk_cli/src/output.rs +++ b/kalk_cli/src/output.rs @@ -4,13 +4,35 @@ use kalk::parser::{self}; pub fn eval(parser: &mut parser::Context, input: &str) { match parser::parse(parser, input, 53) { Ok(Some(result)) => { - if result.clone().fract() == 0 { + if result.is_infinite() { + err("Too big to process."); + } else if result > 100_000_000 || result < -100_000_00 { + let sign = if result >= 0 { "" } else { "-" }; + 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 { - println!("{}", result); + println!( + "{}", + result + .to_string() + .trim_end_matches('0') + .trim_end_matches('.') + ); } } Ok(None) => print!(""), - Err(err) => println!("{}", Red.paint(err)), + Err(msg) => err(&msg), } } + +fn err(msg: &str) { + println!("{}", Red.paint(msg)); +}