mirror of
https://github.com/PaddiM8/kalker.git
synced 2024-12-12 09:30:40 +01:00
Print large numbers accurately
This commit is contained in:
parent
fee73e6b26
commit
d91a10f3fa
@ -1,7 +1,7 @@
|
||||
use ansi_term::Colour::Red;
|
||||
use kalk::{kalk_value::ScientificNotationFormat, parser};
|
||||
|
||||
pub(crate) const DEFAULT_PRECISION: u32 = 63;
|
||||
pub(crate) const DEFAULT_PRECISION: u32 = 1024;
|
||||
|
||||
pub fn eval(parser: &mut parser::Context, input: &str, precision: u32, base: u8, format: ScientificNotationFormat) {
|
||||
match parser::eval(parser, input, precision) {
|
||||
|
@ -33,7 +33,7 @@ macro_rules! float {
|
||||
macro_rules! float {
|
||||
($x:expr) => {{
|
||||
use rug::Float;
|
||||
Float::with_val(63, $x)
|
||||
Float::with_val(1024, $x)
|
||||
}};
|
||||
}
|
||||
|
||||
@ -187,10 +187,10 @@ impl std::fmt::Display for KalkValue {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
KalkValue::Number(real, imaginary, _) => {
|
||||
let as_str = format_number(primitive!(real));
|
||||
let as_str = format_number_big(real);
|
||||
|
||||
if self.has_imaginary() {
|
||||
let imaginary_as_str = format_number(primitive!(imaginary).abs());
|
||||
let imaginary_as_str = format_number_big(&imaginary.clone().abs());
|
||||
let sign = if imaginary < &0f64 { "-" } else { "+" };
|
||||
|
||||
if &as_str == "0" {
|
||||
@ -1155,6 +1155,33 @@ pub fn format_number(input: f64) -> String {
|
||||
spaced(&result)
|
||||
}
|
||||
|
||||
#[cfg(feature = "rug")]
|
||||
pub fn format_number_big(input: &Float) -> String {
|
||||
let input_str = input.to_string();
|
||||
let mut result = if input_str.contains('.') {
|
||||
input_str
|
||||
.trim_end_matches('0')
|
||||
.trim_end_matches('.')
|
||||
.to_string()
|
||||
} else {
|
||||
input_str
|
||||
};
|
||||
|
||||
if let Some(dot_index) = result.find('.') {
|
||||
let decimal_count = result.len() - dot_index;
|
||||
if decimal_count > 10 {
|
||||
result = result[..(result.len() - decimal_count + 10)].to_string();
|
||||
}
|
||||
}
|
||||
|
||||
spaced(&result)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "rug"))]
|
||||
pub fn format_number_big(input: &f64) -> String {
|
||||
format_number(*input)
|
||||
}
|
||||
|
||||
fn calculate_vector(
|
||||
x: KalkValue,
|
||||
y: &KalkValue,
|
||||
|
Loading…
Reference in New Issue
Block a user