Fully removed RUG dependency

This commit is contained in:
2023-09-03 14:36:00 -07:00
parent 38c982bb00
commit 2391606ae1
5 changed files with 216 additions and 106 deletions

View File

@ -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<F64Base> {
return Some(F64Base{ val: f });
}
fn from_string(s: &str) -> Option<F64Base> {
let v = s.parse::<f64>();
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<F64Base> { Some(F64Base{ val: 1f64/self.val.sin() }) }
fn sec(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cos() }) }
fn cot(&self) -> Option<F64Base> { 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<F64Base> { Some(F64Base{ val: 1f64/self.val.sinh() }) }
fn sech(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.cosh() }) }
fn coth(&self) -> Option<F64Base> { Some(F64Base{ val: 1f64/self.val.tanh() }) }
foward!(asinh);
foward!(acosh);
foward!(atanh);

View File

@ -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;

View File

@ -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<Self>;
fn from_string(s: &str) -> Option<Self>;
// 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<Self> {
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<Self> {
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}
}
}