mirror of
https://github.com/rm-dr/daisy
synced 2025-04-19 11:25:59 -07:00
Compare commits
No commits in common. "315be575eeba689dfdad17db2693dbec7ae0ce74" and "682205f5e1ee589249df653637cb41599f1f3bf9" have entirely different histories.
315be575ee
...
682205f5e1
24
Cargo.lock
generated
24
Cargo.lock
generated
@ -8,6 +8,19 @@ version = "1.1.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
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]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
@ -24,6 +37,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||||||
name = "daisycalc"
|
name = "daisycalc"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bigdecimal",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"num",
|
"num",
|
||||||
"termion",
|
"termion",
|
||||||
@ -48,9 +62,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.147"
|
version = "0.2.140"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libm"
|
||||||
|
version = "0.2.7"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
|
@ -28,8 +28,8 @@ cfg-if = "1.0.0"
|
|||||||
|
|
||||||
[target.'cfg(target_family = "unix")'.dependencies]
|
[target.'cfg(target_family = "unix")'.dependencies]
|
||||||
termion = "2.0.1"
|
termion = "2.0.1"
|
||||||
|
bigdecimal = "0.4.1"
|
||||||
num = "0.4.1"
|
num = "0.4.1"
|
||||||
#astro-float = "0.7.1"
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
toml = "0.7.4"
|
toml = "0.7.4"
|
@ -61,7 +61,7 @@ impl Config {
|
|||||||
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
//#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
|
|
||||||
@ -76,12 +76,12 @@ pub struct Context {
|
|||||||
// General functions
|
// General functions
|
||||||
impl Context {
|
impl Context {
|
||||||
pub fn new() -> Context {
|
pub fn new() -> Context {
|
||||||
Context {
|
Context{
|
||||||
config: Config::new(),
|
config: Config::new(),
|
||||||
history: Vec::new(),
|
history: Vec::new(),
|
||||||
variables: HashMap::new(),
|
variables: HashMap::new(),
|
||||||
functions: HashMap::new(),
|
functions: HashMap::new(),
|
||||||
shadow: HashMap::new()
|
shadow: HashMap::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,8 +164,7 @@ pub fn eval_operator(context: &mut Context, g: &Expression) -> Result<Option<Exp
|
|||||||
|
|
||||||
if let Expression::Quantity(la, a) = a {
|
if let Expression::Quantity(la, a) = a {
|
||||||
if let Expression::Quantity(lb, b) = b {
|
if let Expression::Quantity(lb, b) = b {
|
||||||
let o = a.clone() * b.clone();
|
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, a.clone() * b.clone())));
|
||||||
return Ok(Some(Expression::Quantity(*la + *lb + *op_loc, o)));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,33 +27,7 @@ pub struct F64Base where {
|
|||||||
|
|
||||||
impl ToString for F64Base {
|
impl ToString for F64Base {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
// Remove negative sign from string
|
return dec_to_sci(self.val.to_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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,35 +34,7 @@ impl FloatBase {
|
|||||||
|
|
||||||
impl ToString for FloatBase {
|
impl ToString for FloatBase {
|
||||||
fn to_string(&self) -> String {
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,17 +25,36 @@ pub use self::scalar::ScalarBase;
|
|||||||
// Convert a string to scientific notation,
|
// Convert a string to scientific notation,
|
||||||
// with parameters SHOW_SIG and MAX_LEN.
|
// with parameters SHOW_SIG and MAX_LEN.
|
||||||
//
|
//
|
||||||
// input:
|
// input (s): a decimal of any length, like 123123.123123
|
||||||
// neg: true if negative
|
// s may start with an optional `-` sign.
|
||||||
// s: decimal portion. Must contain only digits and a single decimal point.
|
pub(in self) fn dec_to_sci(mut s: String) -> String {
|
||||||
// zeros must be stripped from both ends.
|
// Remove negative sign from string
|
||||||
// p: power of ten to multiply by.
|
let neg = s.starts_with("-");
|
||||||
//
|
if neg { s = String::from(&s[1..]); }
|
||||||
// So, (-1)^(neg) + (s * 10^p) should give us our number.
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
|
|
||||||
// Pick significant digits and round
|
// Pick significant digits and round
|
||||||
|
let mut s = String::from(s);
|
||||||
if s.len() > SHOW_SIG {
|
if s.len() > SHOW_SIG {
|
||||||
let round;
|
let round;
|
||||||
if s.len() != SHOW_SIG + 1 {
|
if s.len() != SHOW_SIG + 1 {
|
||||||
@ -58,8 +77,6 @@ pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
|
|||||||
let neg = if neg {"-"} else {""};
|
let neg = if neg {"-"} else {""};
|
||||||
|
|
||||||
if (p.abs() as usize) < MAX_LEN {
|
if (p.abs() as usize) < MAX_LEN {
|
||||||
// Print whole decimal
|
|
||||||
|
|
||||||
if p >= 0 {
|
if p >= 0 {
|
||||||
let q = p as usize;
|
let q = p as usize;
|
||||||
|
|
||||||
@ -77,9 +94,8 @@ pub(in self) fn dec_to_sci(neg: bool, mut s: String, p: i64) -> String {
|
|||||||
return format!("{neg}{}", t.trim_end_matches('0'));
|
return format!("{neg}{}", t.trim_end_matches('0'));
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
// Print full scientific notation
|
// Print full scientific notation
|
||||||
|
} else {
|
||||||
let first = &s[0..1];
|
let first = &s[0..1];
|
||||||
let mut rest = &s[1..];
|
let mut rest = &s[1..];
|
||||||
rest = rest.trim_end_matches('0');
|
rest = rest.trim_end_matches('0');
|
||||||
|
@ -184,7 +184,7 @@ impl Scalar {
|
|||||||
|
|
||||||
pub fn is_nan(&self) -> bool {
|
pub fn is_nan(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
Scalar::Float{ v } => {v.val.is_nan()},
|
Scalar::Float {..} => {false},
|
||||||
Scalar::Rational {..} => {false}
|
Scalar::Rational {..} => {false}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,6 +275,7 @@ impl Unit {
|
|||||||
let mut q = Quantity::new_rational(1f64).unwrap();
|
let mut q = Quantity::new_rational(1f64).unwrap();
|
||||||
q.set_unit(b);
|
q.set_unit(b);
|
||||||
return Some(q);
|
return Some(q);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user