Compare commits

...

4 Commits

Author SHA1 Message Date
Mark 315be575ee
Fixed small function 2023-09-20 11:09:44 -07:00
Mark 31c368f7d8
Removed dependencies for astrofloat 2023-09-20 11:09:28 -07:00
Mark 8a026fc2ea
Updated scalar print routine 2023-09-20 11:07:47 -07:00
Mark 8497c125ef
Minor cleanup 2023-09-20 11:06:37 -07:00
9 changed files with 78 additions and 60 deletions

24
Cargo.lock generated
View File

@ -8,19 +8,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bigdecimal"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "454bca3db10617b88b566f205ed190aedb0e0e6dd4cad61d3988a72e8c5594cb"
dependencies = [
"autocfg",
"libm",
"num-bigint",
"num-integer",
"num-traits",
]
[[package]]
name = "bitflags"
version = "1.3.2"
@ -37,7 +24,6 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
name = "daisycalc"
version = "1.0.1"
dependencies = [
"bigdecimal",
"cfg-if",
"num",
"termion",
@ -62,15 +48,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.140"
version = "0.2.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "libm"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
[[package]]
name = "memchr"

View File

@ -28,8 +28,8 @@ cfg-if = "1.0.0"
[target.'cfg(target_family = "unix")'.dependencies]
termion = "2.0.1"
bigdecimal = "0.4.1"
num = "0.4.1"
#astro-float = "0.7.1"
[build-dependencies]
toml = "0.7.4"

View File

@ -61,7 +61,7 @@ impl Config {
#[derive(Debug)]
#[derive(Clone)]
//#[derive(Clone)]
pub struct Context {
pub config: Config,
@ -81,7 +81,7 @@ impl Context {
history: Vec::new(),
variables: HashMap::new(),
functions: HashMap::new(),
shadow: HashMap::new(),
shadow: HashMap::new()
}
}

View File

@ -164,7 +164,8 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
if let Expression::Quantity(la, a) = a {
if let Expression::Quantity(lb, b) = b {
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
let o = a.clone() * b.clone();
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, o)));
}
}

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');

View File

@ -184,7 +184,7 @@ impl Scalar {
pub fn is_nan(&self) -> bool {
match self {
Scalar::Float {..} => {false},
Scalar::Float{ v } => {v.val.is_nan()},
Scalar::Rational {..} => {false}
}
}

View File

@ -275,7 +275,6 @@ impl Unit {
let mut q = Quantity::new_rational(1f64).unwrap();
q.set_unit(b);
return Some(q);
}
}