Updated scalar print routine

newfloat
Mark 2023-09-20 11:07:47 -07:00
parent 8497c125ef
commit 8a026fc2ea
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
3 changed files with 69 additions and 31 deletions

View File

@ -27,7 +27,33 @@ pub struct F64Base where {
impl ToString for F64Base {
fn to_string(&self) -> String {
return dec_to_sci(self.val.to_string());
// Remove negative sign from string
let mut s = self.val.to_string();
let neg = s.starts_with("-");
if neg { s = String::from(&s[1..]); }
// Power of ten
let mut p: i64 = {
if let Some(x) = s.find(".") {
x as i64
} else {
s.len() as i64
}
};
p -= 1;
// We no longer need a decimal point in our string.
// also, trim off leading zeros and adjust power.
let mut s: &str = &s.replace(".", "");
s = &s[0..];
s = s.trim_end_matches('0');
while s.starts_with('0') {
s = &s[1..];
p -= 1;
}
return dec_to_sci(neg, s.to_string(), p);
}
}

View File

@ -34,7 +34,35 @@ impl FloatBase {
impl ToString for FloatBase {
fn to_string(&self) -> String {
return dec_to_sci(self.val.to_string());
if self.val.is_nan() {
return "NaN".to_string();
} else if self.val.is_inf_neg() {
return "-Inf".to_string();
} else if self.val.is_inf_pos() {
return "+Inf".to_string();
}
// Already in scientific notation,we just need to trim significant digits.
let mut _a = self.val.round(32, astro_float::RoundingMode::Up).to_string();
let mut _b = _a.split('e');
let mut s = String::from(_b.next().unwrap()); // Decimal
let p: i64 = _b.next().unwrap().parse().unwrap(); // Exponent
// Remove negative sign from string
let neg = s.starts_with("-");
if neg { s = String::from(&s[1..]); }
// We no longer need a decimal point in our string.
// also, trim off leading zeros and adjust power.
let mut s: &str = &s.replace(".", "");
s = &s[0..];
s = s.trim_end_matches('0');
s = s.trim_start_matches('0');
return dec_to_sci(neg, s.to_string(), p);
}
}

View File

@ -25,36 +25,17 @@ pub use self::scalar::ScalarBase;
// Convert a string to scientific notation,
// with parameters SHOW_SIG and MAX_LEN.
//
// input (s): a decimal of any length, like 123123.123123
// s may start with an optional `-` sign.
pub(in self) fn dec_to_sci(mut s: String) -> String {
// Remove negative sign from string
let neg = s.starts_with("-");
if neg { s = String::from(&s[1..]); }
// Power of ten
let mut p: i32 = {
if let Some(x) = s.find(".") {
x as i32
} else {
s.len() as i32
}
};
p -= 1;
// We no longer need a decimal point in our string.
// also, trim off leading zeros and adjust power.
let mut s: &str = &s.replace(".", "");
s = &s[0..];
s = s.trim_end_matches('0');
while s.starts_with('0') {
s = &s[1..];
p -= 1;
}
// input:
// neg: true if negative
// s: decimal portion. Must contain only digits and a single decimal point.
// zeros must be stripped from both ends.
// p: power of ten to multiply by.
//
// So, (-1)^(neg) + (s * 10^p) should give us our number.
#[allow(dead_code)]
pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
// Pick significant digits and round
let mut s = String::from(s);
if s.len() > SHOW_SIG {
let round;
if s.len() != SHOW_SIG + 1 {
@ -77,6 +58,8 @@ pub(in self) fn dec_to_sci(mut s: String) -> String {
let neg = if neg {"-"} else {""};
if (p.abs() as usize) < MAX_LEN {
// Print whole decimal
if p >= 0 {
let q = p as usize;
@ -94,8 +77,9 @@ pub(in self) fn dec_to_sci(mut s: String) -> String {
return format!("{neg}{}", t.trim_end_matches('0'));
}
// Print full scientific notation
} else {
// Print full scientific notation
let first = &s[0..1];
let mut rest = &s[1..];
rest = rest.trim_end_matches('0');