mirror of https://github.com/rm-dr/daisy
Fixed float printing
parent
84ebf89d6f
commit
4353548900
|
@ -8,7 +8,8 @@ use std::ops::{
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use super::ScalarBase;
|
use super::ScalarBase;
|
||||||
use super::PRINT_LEN;
|
use super::SHOW_SIG;
|
||||||
|
use super::MAX_LEN;
|
||||||
|
|
||||||
macro_rules! foward {
|
macro_rules! foward {
|
||||||
( $x:ident ) => {
|
( $x:ident ) => {
|
||||||
|
@ -55,63 +56,57 @@ impl ToString for F64Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Pick significant digits and round
|
||||||
|
let mut s = String::from(s);
|
||||||
|
if s.len() > SHOW_SIG {
|
||||||
|
let round;
|
||||||
|
if s.len() != SHOW_SIG + 1 {
|
||||||
|
round = s[SHOW_SIG..SHOW_SIG+1].parse().unwrap();
|
||||||
|
} else { round = 0; }
|
||||||
|
|
||||||
|
s = String::from(&s[0..SHOW_SIG]);
|
||||||
|
|
||||||
|
if round >= 5 {
|
||||||
|
let new = s[s.len()-1..s.len()].parse::<u8>().unwrap() + 1u8;
|
||||||
|
if new != 10 {
|
||||||
|
s = format!("{}{new}", &s[0..s.len()-1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
s = format!("{s}{}", "0".repeat(SHOW_SIG - s.len()));
|
||||||
|
// at this point, s is guaranteed to have exactly SHOW_SIG digits.
|
||||||
|
|
||||||
let neg = if neg {"-"} else {""};
|
let neg = if neg {"-"} else {""};
|
||||||
|
|
||||||
if (p.abs() as usize) < PRINT_LEN {
|
if (p.abs() as usize) < MAX_LEN {
|
||||||
|
|
||||||
if p >= 0 {
|
if p >= 0 {
|
||||||
let q = p as usize;
|
let q = p as usize;
|
||||||
|
|
||||||
// Add zero padding
|
let first = &s[0..q+1];
|
||||||
let t;
|
let mut rest = &s[q+1..];
|
||||||
if s.len() < (q + 1) {
|
rest = rest.trim_end_matches('0');
|
||||||
t = format!("{s}{}", "0".repeat(q + 1 - s.len()));
|
if rest == "" {
|
||||||
} else { t = s.to_string() }
|
return format!("{neg}{first}");
|
||||||
|
|
||||||
// Section before decimal point
|
|
||||||
let first = &t[0..q+1];
|
|
||||||
|
|
||||||
// The rest of the number, including a decimal point
|
|
||||||
let mut rest: String;
|
|
||||||
if first.len() == t.len() {
|
|
||||||
rest = String::from("");
|
|
||||||
} else {
|
} else {
|
||||||
rest = format!(".{}", &t[q+1..]);
|
return format!("{neg}{first}.{rest}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit length of decimal portion
|
|
||||||
if rest.len() > PRINT_LEN {
|
|
||||||
rest = String::from(&rest[0..PRINT_LEN]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return format!("{neg}{first}{rest}");
|
|
||||||
} else {
|
} else {
|
||||||
let q = p.abs() as usize;
|
let q = p.abs() as usize;
|
||||||
|
let t = format!("0.{}{s}", "0".repeat(q-1));
|
||||||
let t = format!("{neg}0.{}{s}", "0".repeat(q-1));
|
return format!("{neg}{}", t.trim_end_matches('0'));
|
||||||
|
|
||||||
return if t.len() > PRINT_LEN { String::from(&t[0..PRINT_LEN]) } else {t};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print full scientific notation
|
// Print full scientific notation
|
||||||
} else {
|
} else {
|
||||||
// First (non-zero) digit of our number
|
|
||||||
let first = &s[0..1];
|
let first = &s[0..1];
|
||||||
|
let mut rest = &s[1..];
|
||||||
// The rest of the number, including a decimal point
|
rest = rest.trim_end_matches('0');
|
||||||
let mut rest: String;
|
if rest == "" {
|
||||||
if first.len() == s.len() {
|
return format!("{neg}{first}e{p}");
|
||||||
rest = String::from("");
|
|
||||||
} else {
|
} else {
|
||||||
rest = format!(".{}", &s[1..]);
|
return format!("{neg}{first}.{rest}e{p}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit length of decimal portion
|
|
||||||
if rest.len() > 5 {
|
|
||||||
rest = String::from(&rest[0..PRINT_LEN]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return format!("{neg}{first}{rest}e{p}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,11 +244,11 @@ impl Rem<F64Base> for F64Base {
|
||||||
|
|
||||||
fn rem(self, modulus: F64Base) -> Self::Output {
|
fn rem(self, modulus: F64Base) -> Self::Output {
|
||||||
if {
|
if {
|
||||||
(!self.fract().unwrap().is_zero()) ||
|
(!self.is_int()) ||
|
||||||
(!modulus.fract().unwrap().is_zero())
|
(!modulus.is_int())
|
||||||
} { panic!() }
|
} { panic!() }
|
||||||
|
|
||||||
F64Base{val : self.val.fract() % modulus.val.fract()}
|
F64Base{val : self.val.round() % modulus.val.round()}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
//const FLOAT_PRECISION: u32 = 1024;
|
//const FLOAT_PRECISION: u32 = 1024;
|
||||||
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
|
const SHOW_SIG: usize = 5; // How many significant digits we will show in output
|
||||||
|
const MAX_LEN: usize = 5; // If a scientific exponent is >= this value, do not use scientific notation.
|
||||||
|
|
||||||
pub(in self) mod rationalbase;
|
pub(in self) mod rationalbase;
|
||||||
|
|
||||||
|
|
|
@ -131,7 +131,7 @@ fn operators() {
|
||||||
|
|
||||||
good_expr("125", "5^(+3)");
|
good_expr("125", "5^(+3)");
|
||||||
good_expr("125", "+5^3");
|
good_expr("125", "+5^3");
|
||||||
good_expr("0.2148", "3 ^ (-1.4)");
|
good_expr("0.21479", "3 ^ (-1.4)");
|
||||||
|
|
||||||
// Should parse as ((2^3)^4)^5
|
// Should parse as ((2^3)^4)^5
|
||||||
good_expr("1.1529e18", "2^3^4^5");
|
good_expr("1.1529e18", "2^3^4^5");
|
||||||
|
|
Loading…
Reference in New Issue