mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-01-08 06:28:56 +01:00
Output numbers with the precision specified
This commit is contained in:
parent
6ad51c82ea
commit
535a834076
@ -51,7 +51,9 @@ fn default_action(context: &Context) {
|
|||||||
let mut parser_context = parser::Context::new()
|
let mut parser_context = parser::Context::new()
|
||||||
.set_angle_unit(&angle_unit)
|
.set_angle_unit(&angle_unit)
|
||||||
.set_timeout(None);
|
.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") {
|
if let Ok(input_file_path) = context.string_flag("input-file") {
|
||||||
load_input_file(&input_file_path, precision, &mut parser_context);
|
load_input_file(&input_file_path, precision, &mut parser_context);
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
use ansi_term::Colour::Red;
|
use ansi_term::Colour::Red;
|
||||||
use kalk::parser;
|
use kalk::parser;
|
||||||
|
|
||||||
|
pub(crate) const DEFAULT_PRECISION: u32 = 63;
|
||||||
|
|
||||||
pub fn eval(parser: &mut parser::Context, input: &str, precision: u32) {
|
pub fn eval(parser: &mut parser::Context, input: &str, precision: u32) {
|
||||||
match parser::eval(parser, input, precision) {
|
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!(""),
|
Ok(None) => print!(""),
|
||||||
Err(err) => print_err(&err.to_string()),
|
Err(err) => print_err(&err.to_string()),
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,11 @@ pub struct KalkNum {
|
|||||||
|
|
||||||
impl KalkNum {
|
impl KalkNum {
|
||||||
pub fn new(value: Float, unit: &str) -> Self {
|
pub fn new(value: Float, unit: &str) -> Self {
|
||||||
|
let precision = value.prec();
|
||||||
Self {
|
Self {
|
||||||
value,
|
value,
|
||||||
unit: unit.to_string(),
|
unit: unit.to_string(),
|
||||||
imaginary_value: Float::with_val(63, 0),
|
imaginary_value: Float::with_val(precision, 0),
|
||||||
boolean_value: None,
|
boolean_value: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +40,7 @@ impl KalkNum {
|
|||||||
|
|
||||||
pub fn from_imaginary(value: Float) -> Self {
|
pub fn from_imaginary(value: Float) -> Self {
|
||||||
Self {
|
Self {
|
||||||
value: Float::with_val(63, 0),
|
value: Float::with_val(value.prec(), 0),
|
||||||
unit: String::new(),
|
unit: String::new(),
|
||||||
imaginary_value: value,
|
imaginary_value: value,
|
||||||
boolean_value: None,
|
boolean_value: None,
|
||||||
|
Loading…
Reference in New Issue
Block a user