Output numbers with the precision specified

This commit is contained in:
PaddiM8 2021-09-22 16:23:10 +02:00 committed by PaddiM8
parent 6ad51c82ea
commit 535a834076
3 changed files with 15 additions and 4 deletions

View File

@ -51,7 +51,9 @@ fn default_action(context: &Context) {
let mut parser_context = parser::Context::new()
.set_angle_unit(&angle_unit)
.set_timeout(None);
let precision = context.int_flag("precision").unwrap_or(53isize) as u32;
let precision = context
.int_flag("precision")
.unwrap_or(output::DEFAULT_PRECISION as isize) as u32;
if let Ok(input_file_path) = context.string_flag("input-file") {
load_input_file(&input_file_path, precision, &mut parser_context);

View File

@ -1,9 +1,17 @@
use ansi_term::Colour::Red;
use kalk::parser;
pub(crate) const DEFAULT_PRECISION: u32 = 63;
pub fn eval(parser: &mut parser::Context, input: &str, precision: u32) {
match parser::eval(parser, input, precision) {
Ok(Some(result)) => println!("{}", result.to_string_pretty()),
Ok(Some(result)) => {
if precision == DEFAULT_PRECISION {
println!("{}", result.to_string_pretty())
} else {
println!("{}", result.to_string_big())
}
}
Ok(None) => print!(""),
Err(err) => print_err(&err.to_string()),
}

View File

@ -16,10 +16,11 @@ pub struct KalkNum {
impl KalkNum {
pub fn new(value: Float, unit: &str) -> Self {
let precision = value.prec();
Self {
value,
unit: unit.to_string(),
imaginary_value: Float::with_val(63, 0),
imaginary_value: Float::with_val(precision, 0),
boolean_value: None,
}
}
@ -39,7 +40,7 @@ impl KalkNum {
pub fn from_imaginary(value: Float) -> Self {
Self {
value: Float::with_val(63, 0),
value: Float::with_val(value.prec(), 0),
unit: String::new(),
imaginary_value: value,
boolean_value: None,