diff --git a/Cargo.lock b/Cargo.lock index 2cbc20d..c133079 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,10 +9,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] -name = "az" -version = "1.2.1" +name = "bigdecimal" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973" +checksum = "454bca3db10617b88b566f205ed190aedb0e0e6dd4cad61d3988a72e8c5594cb" +dependencies = [ + "autocfg", + "libm", + "num-bigint", + "num-integer", + "num-traits", +] [[package]] name = "bitflags" @@ -30,22 +37,13 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "daisycalc" version = "1.0.1" dependencies = [ + "bigdecimal", "cfg-if", - "rug", + "num", "termion", "toml", ] -[[package]] -name = "gmp-mpfr-sys" -version = "1.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b560063e2ffa8ce9c2ef9bf487f2944a97deca5b8de0b5bcd0ae6437ef8b75f" -dependencies = [ - "libc", - "windows-sys", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -68,12 +66,94 @@ version = "0.2.140" 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" + [[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +[[package]] +name = "num" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05180d69e3da0e530ba2a1dae5110317e49e3b7f3d41be227dc5f92e49ee7af" +dependencies = [ + "num-bigint", + "num-complex", + "num-integer", + "num-iter", + "num-rational", + "num-traits", +] + +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-iter" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" +dependencies = [ + "autocfg", +] + [[package]] name = "numtoa" version = "0.1.0" @@ -98,17 +178,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "rug" -version = "1.19.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "555e8b44763d034526db899c88cd56ccc4486cd38b444c8aa0e79d4e70ae5a34" -dependencies = [ - "az", - "gmp-mpfr-sys", - "libc", -] - [[package]] name = "serde" version = "1.0.164" @@ -170,63 +239,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "windows-sys" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - -[[package]] -name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - -[[package]] -name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" - [[package]] name = "winnow" version = "0.4.6" diff --git a/Cargo.toml b/Cargo.toml index d611062..b08301d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,8 @@ cfg-if = "1.0.0" [target.'cfg(target_family = "unix")'.dependencies] termion = "2.0.1" -rug = "1.19.2" +bigdecimal = "0.4.1" +num = "0.4.1" [build-dependencies] toml = "0.7.4" \ No newline at end of file diff --git a/src/quantity/scalar/f64base.rs b/src/quantity/scalar/f64base.rs index 3901920..0de90bd 100644 --- a/src/quantity/scalar/f64base.rs +++ b/src/quantity/scalar/f64base.rs @@ -8,7 +8,7 @@ use std::ops::{ use std::cmp::Ordering; use super::ScalarBase; - +use super::PRINT_LEN; macro_rules! foward { ( $x:ident ) => { @@ -25,16 +25,99 @@ pub struct F64Base where { } impl ToString for F64Base { - fn to_string(&self) -> String { self.val.to_string() } + fn to_string(&self) -> String { + // Decimal, looks like xxx.xxx. + // May start with a zero. + let mut s = self.val.to_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; + } + + + let neg = if neg {"-"} else {""}; + + if (p.abs() as usize) < PRINT_LEN { + + if p >= 0 { + let q = p as usize; + + // Add zero padding + let t; + if s.len() < (q + 1) { + t = format!("{s}{}", "0".repeat(q + 1 - s.len())); + } else { t = s.to_string() } + + // 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 { + rest = format!(".{}", &t[q+1..]); + } + + // Limit length of decimal portion + if rest.len() > PRINT_LEN { + rest = String::from(&rest[0..PRINT_LEN]); + } + + return format!("{neg}{first}{rest}"); + } else { + let q = p.abs() as usize; + + let t = format!("{neg}0.{}{s}", "0".repeat(q-1)); + + return if t.len() > PRINT_LEN { String::from(&t[0..PRINT_LEN]) } else {t}; + } + + // Print full scientific notation + } else { + // First (non-zero) digit of our number + let first = &s[0..1]; + + // The rest of the number, including a decimal point + let mut rest: String; + if first.len() == s.len() { + rest = String::from(""); + } else { + rest = format!(".{}", &s[1..]); + } + + // Limit length of decimal portion + if rest.len() > 5 { + rest = String::from(&rest[0..PRINT_LEN]); + } + + return format!("{neg}{first}{rest}e{p}"); + } + } } impl ScalarBase for F64Base { - - fn from_f64(f: f64) -> Option { - return Some(F64Base{ val: f }); - } - fn from_string(s: &str) -> Option { let v = s.parse::(); let v = match v { @@ -51,6 +134,7 @@ impl ScalarBase for F64Base { fn is_one(&self) -> bool {self.val == 1f64} fn is_negative(&self) -> bool { self.val.is_sign_negative() } fn is_positive(&self) -> bool { self.val.is_sign_positive() } + fn is_int(&self) -> bool { self.val.floor() == self.val } foward!(abs); foward!(floor); @@ -60,9 +144,11 @@ impl ScalarBase for F64Base { foward!(sin); foward!(cos); foward!(tan); - foward!(csc); - foward!(sec); - foward!(cot); + + fn csc(&self) -> Option { Some(F64Base{ val: 1f64/self.val.sin() }) } + fn sec(&self) -> Option { Some(F64Base{ val: 1f64/self.val.cos() }) } + fn cot(&self) -> Option { Some(F64Base{ val: 1f64/self.val.tan() }) } + foward!(asin); foward!(acos); foward!(atan); @@ -70,9 +156,11 @@ impl ScalarBase for F64Base { foward!(sinh); foward!(cosh); foward!(tanh); - foward!(csch); - foward!(sech); - foward!(coth); + + fn csch(&self) -> Option { Some(F64Base{ val: 1f64/self.val.sinh() }) } + fn sech(&self) -> Option { Some(F64Base{ val: 1f64/self.val.cosh() }) } + fn coth(&self) -> Option { Some(F64Base{ val: 1f64/self.val.tanh() }) } + foward!(asinh); foward!(acosh); foward!(atanh); diff --git a/src/quantity/scalar/mod.rs b/src/quantity/scalar/mod.rs index 65a4722..b3f4f32 100644 --- a/src/quantity/scalar/mod.rs +++ b/src/quantity/scalar/mod.rs @@ -1,9 +1,19 @@ -const FLOAT_PRECISION: u32 = 1024; +//const FLOAT_PRECISION: u32 = 1024; const PRINT_LEN: usize = 5; // How many significant digits we will show in output pub(in self) mod rationalbase; -pub(in self) mod floatbase; -//mod f64base; + + +// Pick a float implementation. +// floatbase is high-precision, f64base is for testing. + +//pub(in self) mod floatbase; +//pub use floatbase::FloatBase; + +pub(in self) mod f64base; +pub use f64base::F64Base as FloatBase; + + mod scalar; pub use self::scalar::Scalar; diff --git a/src/quantity/scalar/scalar.rs b/src/quantity/scalar/scalar.rs index 756e5bb..e6a0d5a 100644 --- a/src/quantity/scalar/scalar.rs +++ b/src/quantity/scalar/scalar.rs @@ -7,7 +7,7 @@ use std::ops::{ }; use std::cmp::Ordering; -use super::floatbase::FloatBase as FloatBase; +use super::FloatBase as FloatBase; use super::rationalbase::RationalBase; @@ -21,7 +21,6 @@ pub trait ScalarBase: PartialEq + PartialOrd { // Creation - fn from_f64(f: f64) -> Option; fn from_string(s: &str) -> Option; // Utility @@ -87,8 +86,8 @@ fn to_float(r: Scalar) -> Scalar { match &r { Scalar::Float {..} => r, Scalar::Rational {v} => wrap_float!( - FloatBase::from(v.val.numer()).unwrap() / - FloatBase::from(v.val.denom()).unwrap() + FloatBase::from_string(&v.val.numer().to_string()).unwrap() / + FloatBase::from_string(&v.val.denom().to_string()).unwrap() ) } } @@ -105,13 +104,13 @@ impl ToString for Scalar { // Creation methods impl Scalar { pub fn new_float(f: f64) -> Option { - let v = FloatBase::from_f64(f); + let v = FloatBase::from_string(&f.to_string()); if v.is_none() { return None; } return Some(wrap_float!(v.unwrap())); } pub fn new_rational(f: f64) -> Option { - let r = RationalBase::from_f64(f); + let r = RationalBase::from_string(&f.to_string()); if r.is_none() { return None; } return Some(wrap_rational!(r.unwrap())); } @@ -185,7 +184,7 @@ impl Scalar { pub fn is_nan(&self) -> bool { match self { - Scalar::Float {v} => {v.val.is_nan()}, + Scalar::Float {..} => {false}, Scalar::Rational {..} => {false} } }