Reorganized quantity

This commit is contained in:
2023-04-08 20:26:07 -07:00
parent 5b8dd2f703
commit fb9cc03bb9
8 changed files with 655 additions and 610 deletions

View File

@ -0,0 +1,175 @@
use std::ops::{
Add, Sub, Mul, Div,
Neg, Rem,
AddAssign, SubAssign,
MulAssign, DivAssign
};
use std::cmp::Ordering;
use super::ScalarBase;
macro_rules! foward {
( $x:ident ) => {
fn $x(&self) -> Option<F64Base> {
Some(F64Base{ val: self.val.clone().$x() })
}
}
}
#[derive(Debug)]
#[derive(Clone)]
pub struct F64Base where {
pub val: f64
}
impl ToString for F64Base {
fn to_string(&self) -> String { self.val.to_string() }
}
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 {
Ok(x) => x,
Err(_) => return None
};
return Some(F64Base{ val: v });
}
foward!(fract);
fn is_zero(&self) -> bool {self.val == 0f64}
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
foward!(abs);
foward!(floor);
foward!(ceil);
foward!(round);
foward!(sin);
foward!(cos);
foward!(tan);
foward!(asin);
foward!(acos);
foward!(atan);
foward!(sinh);
foward!(cosh);
foward!(tanh);
foward!(asinh);
foward!(acosh);
foward!(atanh);
foward!(exp);
foward!(ln);
foward!(log10);
foward!(log2);
fn log(&self, base: Self) -> Option<Self> {
Some(F64Base{ val: self.val.clone().log10() } / base.log10().unwrap())
}
fn pow(&self, base: Self) -> Option<Self> {
Some(F64Base{ val: self.val.clone().powf(base.val)})
}
}
impl Add for F64Base where {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self { val: self.val + other.val}
}
}
impl AddAssign for F64Base where {
fn add_assign(&mut self, other: Self) {
self.val += other.val;
}
}
impl Sub for F64Base {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {val: self.val - other.val}
}
}
impl SubAssign for F64Base where {
fn sub_assign(&mut self, other: Self) {
self.val -= other.val;
}
}
impl Mul for F64Base {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
Self {val: self.val * other.val}
}
}
impl MulAssign for F64Base where {
fn mul_assign(&mut self, other: Self) {
self.val *= other.val;
}
}
impl Div for F64Base {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
Self {val: self.val / other.val}
}
}
impl DivAssign for F64Base where {
fn div_assign(&mut self, other: Self) {
self.val /= other.val;
}
}
impl Neg for F64Base where {
type Output = Self;
fn neg(self) -> Self::Output {
Self {val: -self.val}
}
}
impl Rem<F64Base> for F64Base {
type Output = Self;
fn rem(self, modulus: F64Base) -> Self::Output {
if {
(!self.fract().unwrap().is_zero()) ||
(!modulus.fract().unwrap().is_zero())
} { panic!() }
F64Base{val : self.val.fract() % modulus.val.fract()}
}
}
impl PartialEq for F64Base {
fn eq(&self, other: &Self) -> bool {
self.val == other.val
}
}
impl PartialOrd for F64Base {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.val.partial_cmp(&other.val)
}
}

View File

@ -0,0 +1,251 @@
use rug::Float;
use rug::Assign;
use rug::ops::AssignRound;
use rug::ops::Pow;
use std::ops::{
Add, Sub, Mul, Div,
Neg, Rem,
AddAssign, SubAssign,
MulAssign, DivAssign
};
use std::cmp::Ordering;
use super::ScalarBase;
use super::PRINT_LEN;
use super::FLOAT_PRECISION;
macro_rules! foward {
( $x:ident ) => {
fn $x(&self) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().$x()})
}
}
}
#[derive(Debug)]
#[derive(Clone)]
pub struct FloatBase where {
pub val: Float
}
impl FloatBase {
pub fn from<T>(a: T) -> Option<FloatBase> where
Float: Assign<T> + AssignRound<T>
{
let v = Float::with_val(FLOAT_PRECISION, a);
return Some(FloatBase{ val: v });
}
}
impl ToString for FloatBase {
fn to_string(&self) -> String {
let (sign, mut string, exp) = self.val.to_sign_string_exp(10, Some(PRINT_LEN));
// zero, nan, or inf.
let sign = if sign {"-"} else {""};
if exp.is_none() { return format!("{sign}{string}"); }
let exp = exp.unwrap();
// Remove trailing zeros.
// At this point, string is guaranteed to be nonzero.
while string.chars().last().unwrap() == '0' {
string.remove(string.len() - 1);
}
let exp_u: usize;
if exp < 0 {
exp_u = (-exp).try_into().unwrap()
} else {
exp_u = exp.try_into().unwrap()
}
if exp_u >= PRINT_LEN {
// Exponential notation
let pre = &string[0..1];
let post = &string[1..];
format!(
"{pre}{}{post}e{}",
if post.len() != 0 {"."} else {""},
//if exp > 0 {"+"} else {""},
exp - 1
)
} else {
if exp <= 0 { // Decimal, needs `0.` and leading zeros
format!(
"{sign}0.{}{string}",
"0".repeat(exp_u)
)
} else if exp_u < string.len() { // Decimal, needs only `.`
format!(
"{sign}{}.{}",
&string[0..exp_u],
&string[exp_u..]
)
} else { // Integer, needs trailing zeros
format!(
"{sign}{string}{}",
"0".repeat(exp_u - string.len())
)
}
}
}
}
impl ScalarBase for FloatBase {
fn from_f64(f: f64) -> Option<FloatBase> {
let v = Float::with_val(FLOAT_PRECISION, f);
return Some(FloatBase{ val: v });
}
fn from_string(s: &str) -> Option<FloatBase> {
let v = Float::parse(s);
let v = match v {
Ok(x) => x,
Err(_) => return None
};
return Some(
FloatBase{ val:
Float::with_val(FLOAT_PRECISION, v)
}
);
}
foward!(fract);
fn is_zero(&self) -> bool {self.val.is_zero()}
fn is_negative(&self) -> bool { self.val.is_sign_negative() }
fn is_positive(&self) -> bool { self.val.is_sign_positive() }
foward!(abs);
foward!(floor);
foward!(ceil);
foward!(round);
foward!(sin);
foward!(cos);
foward!(tan);
foward!(asin);
foward!(acos);
foward!(atan);
foward!(sinh);
foward!(cosh);
foward!(tanh);
foward!(asinh);
foward!(acosh);
foward!(atanh);
foward!(exp);
foward!(ln);
foward!(log10);
foward!(log2);
fn log(&self, base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().log10() } / base.log10().unwrap())
}
fn pow(&self, base: FloatBase) -> Option<FloatBase> {
Some(FloatBase{ val: self.val.clone().pow(base.val)})
}
}
impl Add for FloatBase where {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self { val: self.val + other.val}
}
}
impl AddAssign for FloatBase where {
fn add_assign(&mut self, other: Self) {
self.val += other.val;
}
}
impl Sub for FloatBase {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {val: self.val - other.val}
}
}
impl SubAssign for FloatBase where {
fn sub_assign(&mut self, other: Self) {
self.val -= other.val;
}
}
impl Mul for FloatBase {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
Self {val: self.val * other.val}
}
}
impl MulAssign for FloatBase where {
fn mul_assign(&mut self, other: Self) {
self.val *= other.val;
}
}
impl Div for FloatBase {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
Self {val: self.val / other.val}
}
}
impl DivAssign for FloatBase where {
fn div_assign(&mut self, other: Self) {
self.val /= other.val;
}
}
impl Neg for FloatBase where {
type Output = Self;
fn neg(self) -> Self::Output {
Self {val: -self.val}
}
}
impl Rem<FloatBase> for FloatBase {
type Output = Self;
fn rem(self, modulus: FloatBase) -> Self::Output {
if {
(!self.fract().unwrap().is_zero()) ||
(!modulus.fract().unwrap().is_zero())
} { panic!() }
FloatBase{val : self.val.fract() % modulus.val.fract()}
}
}
impl PartialEq for FloatBase {
fn eq(&self, other: &Self) -> bool {
self.val == other.val
}
}
impl PartialOrd for FloatBase {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.val.partial_cmp(&other.val)
}
}

382
src/quantity/scalar/mod.rs Normal file
View File

@ -0,0 +1,382 @@
use std::ops::{
Add, Sub, Mul, Div,
Neg, Rem,
AddAssign, SubAssign,
MulAssign, DivAssign
};
use std::cmp::Ordering;
pub trait ScalarBase:
Sized + ToString +
Add + AddAssign +
Sub + SubAssign +
Mul + MulAssign +
Div + DivAssign +
Neg + Rem +
PartialEq + PartialOrd
{
// Creation
fn from_f64(f: f64) -> Option<Self>;
fn from_string(s: &str) -> Option<Self>;
// Utility
fn fract(&self) -> Option<Self>;
fn is_zero(&self) -> bool;
fn is_negative(&self) -> bool;
fn is_positive(&self) -> bool;
// Mathematical
fn exp(&self) -> Option<Self>;
fn abs(&self) -> Option<Self>;
fn floor(&self) -> Option<Self>;
fn ceil(&self) -> Option<Self>;
fn round(&self) -> Option<Self>;
fn sin(&self) -> Option<Self>;
fn cos(&self) -> Option<Self>;
fn tan(&self) -> Option<Self>;
fn asin(&self) -> Option<Self>;
fn acos(&self) -> Option<Self>;
fn atan(&self) -> Option<Self>;
fn sinh(&self) -> Option<Self>;
fn cosh(&self) -> Option<Self>;
fn tanh(&self) -> Option<Self>;
fn asinh(&self) -> Option<Self>;
fn acosh(&self) -> Option<Self>;
fn atanh(&self) -> Option<Self>;
fn ln(&self) -> Option<Self>;
fn log10(&self) -> Option<Self>;
fn log2(&self) -> Option<Self>;
fn log(&self, base: Self) -> Option<Self>;
fn pow(&self, exp: Self) -> Option<Self>;
}
const FLOAT_PRECISION: u32 = 1024;
const PRINT_LEN: usize = 5; // How many significant digits we will show in output
mod rationalbase;
mod floatbase;
//mod f64base;
use self::rationalbase::RationalBase;
use self::floatbase::FloatBase as FloatBase;
#[derive(Debug)]
#[derive(Clone)]
pub enum Scalar {
Rational{ v: RationalBase },
Float{ v: FloatBase }
}
macro_rules! wrap_rational {
( $x:expr) => { Scalar::Rational{v: $x} }
}
macro_rules! wrap_float {
( $x:expr) => { Scalar::Float{v: $x} }
}
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()
)
}
}
impl ToString for Scalar {
fn to_string(&self) -> String {
match self {
Scalar::Rational{..} => to_float(self.clone()).to_string(),
Scalar::Float{v} => v.to_string()
}
}
}
// Creation methods
impl Scalar {
pub fn new_float(f: f64) -> Option<Self> {
let v = FloatBase::from_f64(f);
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);
if r.is_none() { return None; }
return Some(wrap_rational!(r.unwrap()));
}
pub fn new_rational_from_string(s: &str) -> Option<Self> {
let r = RationalBase::from_string(s);
if r.is_none() { return None; }
return Some(wrap_rational!(r.unwrap()));
}
pub fn new_float_from_string(s: &str) -> Option<Self> {
let v = FloatBase::from_string(s);
if v.is_none() { return None; }
return Some(wrap_float!(v.unwrap()))
}
}
impl Scalar {
pub fn is_nan(&self) -> bool {
match self {
Scalar::Float {v} => {v.val.is_nan()},
Scalar::Rational {..} => {panic!()}
}
}
}
// Forwarded functions
macro_rules! scalar_foward {
( $x:ident ) => {
pub fn $x(&self) -> Scalar {
match self {
Scalar::Rational{v} => {
let r = v.$x();
if r.is_none() {
let v = to_float(self.clone());
return v.$x();
} else {wrap_rational!(r.unwrap())}
},
Scalar::Float{v} => {wrap_float!(v.$x().unwrap())},
}
}
}
}
impl Scalar {
pub fn is_zero(&self) -> bool {
match self {
Scalar::Rational{v} => v.is_zero(),
Scalar::Float{v} => v.is_zero(),
}
}
pub fn is_negative(&self) -> bool {
match self {
Scalar::Rational{v} => v.is_negative(),
Scalar::Float{v} => v.is_negative(),
}
}
pub fn is_positive(&self) -> bool {
match self {
Scalar::Rational{v} => v.is_positive(),
Scalar::Float{v} => v.is_positive(),
}
}
scalar_foward!(fract);
scalar_foward!(abs);
scalar_foward!(floor);
scalar_foward!(ceil);
scalar_foward!(round);
scalar_foward!(sin);
scalar_foward!(cos);
scalar_foward!(tan);
scalar_foward!(asin);
scalar_foward!(acos);
scalar_foward!(atan);
scalar_foward!(sinh);
scalar_foward!(cosh);
scalar_foward!(tanh);
scalar_foward!(asinh);
scalar_foward!(acosh);
scalar_foward!(atanh);
scalar_foward!(exp);
scalar_foward!(ln);
scalar_foward!(log10);
scalar_foward!(log2);
pub fn log(&self, base: Scalar) -> Scalar {
match self {
Scalar::Rational{..} => { to_float(self.clone()).log(to_float(base)) },
Scalar::Float{..} => { to_float(self.clone()).log(to_float(base)) },
}
}
pub fn pow(&self, base: Scalar) -> Scalar {
match self {
Scalar::Rational{..} => {
let a = match to_float(self.clone()) {
Scalar::Rational{..} => panic!(),
Scalar::Float{v} => v,
};
let b = match to_float(base) {
Scalar::Rational{..} => panic!(),
Scalar::Float{v} => v,
};
wrap_float!(a.pow(b).unwrap())
},
Scalar::Float{..} => {
let a = match to_float(self.clone()) {
Scalar::Rational{..} => panic!(),
Scalar::Float{v} => v,
};
let b = match to_float(base) {
Scalar::Rational{..} => panic!(),
Scalar::Float{v} => v,
};
wrap_float!(a.pow(b).unwrap())
},
}
}
}
impl Neg for Scalar where {
type Output = Self;
fn neg(self) -> Self::Output {
match self {
Scalar::Float { v } => {wrap_float!(-v)},
Scalar::Rational { v } => {wrap_rational!(-v)},
}
}
}
impl Add for Scalar {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
match (&self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {wrap_float!(va.clone()+vb.clone())},
(Scalar::Float{..}, Scalar::Rational{..}) => {self + to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self) + other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {wrap_rational!(va.clone()+vb.clone())},
}
}
}
impl AddAssign for Scalar where {
fn add_assign(&mut self, other: Self) {
match (&mut *self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {*va += vb.clone()},
(Scalar::Float{..}, Scalar::Rational{..}) => {*self += to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {*self = to_float(self.clone()) + other },
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {*va += vb.clone()},
}
}
}
impl Sub for Scalar {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
match (&self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {wrap_float!(va.clone()-vb.clone())},
(Scalar::Float{..}, Scalar::Rational{..}) => {self - to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self) - other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {wrap_rational!(va.clone()-vb.clone())},
}
}
}
impl SubAssign for Scalar where {
fn sub_assign(&mut self, other: Self) {
match (&mut *self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {*va -= vb.clone()},
(Scalar::Float{..}, Scalar::Rational{..}) => {*self -= to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {*self = to_float(self.clone()) - other },
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {*va -= vb.clone()},
}
}
}
impl Mul for Scalar {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
match (&self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {wrap_float!(va.clone()*vb.clone())},
(Scalar::Float{..}, Scalar::Rational{..}) => {self * to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self) * other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {wrap_rational!(va.clone()*vb.clone())},
}
}
}
impl MulAssign for Scalar where {
fn mul_assign(&mut self, other: Self) {
match (&mut *self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {*va *= vb.clone()},
(Scalar::Float{..}, Scalar::Rational{..}) => {*self *= to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {*self = to_float(self.clone()) * other },
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {*va *= vb.clone()},
}
}
}
impl Div for Scalar {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
match (&self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {wrap_float!(va.clone()/vb.clone())},
(Scalar::Float{..}, Scalar::Rational{..}) => {self / to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self) / other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {wrap_rational!(va.clone()/vb.clone())},
}
}
}
impl DivAssign for Scalar where {
fn div_assign(&mut self, other: Self) {
match (&mut *self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {*va /= vb.clone()},
(Scalar::Float{..}, Scalar::Rational{..}) => {*self /= to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {*self = to_float(self.clone()) / other },
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {*va /= vb.clone()},
}
}
}
impl Rem<Scalar> for Scalar {
type Output = Self;
fn rem(self, other: Scalar) -> Self::Output {
match (&self, &other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {wrap_float!(va.clone()%vb.clone())},
(Scalar::Float{..}, Scalar::Rational{..}) => {self % to_float(other)},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self) % other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {wrap_rational!(va.clone()%vb.clone())},
}
}
}
impl PartialEq for Scalar {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => { va == vb },
(Scalar::Float{..}, Scalar::Rational{..}) => {*self == to_float(other.clone())},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self.clone()) == *other},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => { va == vb },
}
}
}
impl PartialOrd for Scalar {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(Scalar::Float{v:va}, Scalar::Float{v:vb}) => {va.partial_cmp(vb)},
(Scalar::Float{..}, Scalar::Rational{..}) => {(*self).partial_cmp(&to_float(other.clone()))},
(Scalar::Rational{..}, Scalar::Float{..}) => {to_float(self.clone()).partial_cmp(other)},
(Scalar::Rational{v:va}, Scalar::Rational{v:vb}) => {va.partial_cmp(vb)},
}
}
}

View File

@ -0,0 +1,248 @@
use rug::Rational;
use rug::Integer;
use std::ops::{
Add, Sub, Mul, Div,
Neg, Rem,
AddAssign, SubAssign,
MulAssign, DivAssign
};
use std::cmp::Ordering;
use super::ScalarBase;
macro_rules! cant_do {
( $x:ident ) => {
fn $x(&self) -> Option<RationalBase> { None }
}
}
#[derive(Debug)]
#[derive(Clone)]
pub struct RationalBase where {
pub val: Rational
}
/*
fn to_string_radix(&self, radix: i32, num_digits: Option<usize>) -> String {
self.to_float().to_string_radix(radix, num_digits)
}
fn to_sign_string_exp(&self, radix: i32, num_digits: Option<usize>) -> (bool, String, Option<i32>) {
self.to_float().to_sign_string_exp(radix, num_digits)
}
*/
impl ToString for RationalBase{
fn to_string(&self) -> String {
return self.val.to_string();
}
}
impl ScalarBase for RationalBase {
fn from_f64(f: f64) -> Option<RationalBase> {
let v = Rational::from_f64(f);
if v.is_none() { return None }
return Some(RationalBase{ val: v.unwrap() });
}
fn from_string(s: &str) -> Option<RationalBase> {
// Scientific notation
let mut sci = s.split("e");
let num = sci.next().unwrap();
let exp = sci.next();
let exp = if exp.is_some() {
let r = exp.unwrap().parse::<isize>();
match r {
Ok(x) => x,
Err(_) => return None
}
} else {0isize};
// Split integer and decimal parts
let mut dec = num.split(".");
let a = dec.next().unwrap();
let b = dec.next();
let b = if b.is_some() {b.unwrap()} else {""};
// Error conditions
if {
dec.next().is_some() || // We should have at most one `.`
sci.next().is_some() || // We should have at most one `e`
a.len() == 0 // We need something in the numerator
} { return None; }
let s: String;
if exp < 0 {
let exp: usize = (-exp).try_into().unwrap();
s = format!("{a}{b}/1{}", "0".repeat(b.len() + exp));
} else if exp > 0 {
let exp: usize = exp.try_into().unwrap();
s = format!(
"{a}{b}{}/1{}",
"0".repeat(exp),
"0".repeat(b.len())
);
} else { // exp == 0
s = format!("{a}{b}/1{}", "0".repeat(b.len()));
};
// From fraction string
let r = Rational::from_str_radix(&s, 10);
let r = match r {
Ok(x) => x,
Err(_) => return None
};
return Some(RationalBase{val: r});
}
fn fract(&self) -> Option<RationalBase> {
Some(RationalBase{val: self.val.clone().fract_floor(Integer::new()).0})
}
fn is_zero(&self) -> bool {self.val == Rational::from((0,1))}
fn is_negative(&self) -> bool { self.val.clone().signum() == -1 }
fn is_positive(&self) -> bool { self.val.clone().signum() == 1 }
fn abs(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().abs()})}
fn floor(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().floor()})}
fn ceil(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().ceil()})}
fn round(&self) -> Option<RationalBase> {Some(RationalBase{val: self.val.clone().round()})}
cant_do!(sin);
cant_do!(cos);
cant_do!(tan);
cant_do!(asin);
cant_do!(acos);
cant_do!(atan);
cant_do!(sinh);
cant_do!(cosh);
cant_do!(tanh);
cant_do!(asinh);
cant_do!(acosh);
cant_do!(atanh);
cant_do!(exp);
cant_do!(ln);
cant_do!(log10);
cant_do!(log2);
fn log(&self, _base: RationalBase) -> Option<RationalBase> { None }
fn pow(&self, _base: RationalBase) -> Option<RationalBase> { None }
}
impl Add for RationalBase where {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
val: self.val + other.val
}
}
}
impl AddAssign for RationalBase where {
fn add_assign(&mut self, other: Self) {
self.val += other.val;
}
}
impl Sub for RationalBase {
type Output = Self;
fn sub(self, other: Self) -> Self::Output {
Self {
val: self.val - other.val
}
}
}
impl SubAssign for RationalBase where {
fn sub_assign(&mut self, other: Self) {
self.val -= other.val;
}
}
impl Mul for RationalBase {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
Self {
val: self.val * other.val
}
}
}
impl MulAssign for RationalBase where {
fn mul_assign(&mut self, other: Self) {
self.val *= other.val;
}
}
impl Div for RationalBase {
type Output = Self;
fn div(self, other: Self) -> Self::Output {
Self {
val: self.val / other.val
}
}
}
impl DivAssign for RationalBase where {
fn div_assign(&mut self, other: Self) {
self.val /= other.val;
}
}
impl Neg for RationalBase where {
type Output = Self;
fn neg(self) -> Self::Output {
Self {
val: -self.val
}
}
}
impl Rem<RationalBase> for RationalBase {
type Output = Self;
fn rem(self, modulus: RationalBase) -> Self::Output {
if {
*self.val.denom() != 1 ||
*modulus.val.denom() != 1
} { panic!() }
RationalBase{
val : Rational::from((
self.val.numer() % modulus.val.numer(),
1
))
}
}
}
impl PartialEq for RationalBase {
fn eq(&self, other: &Self) -> bool {
self.val == other.val
}
}
impl PartialOrd for RationalBase {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.val.partial_cmp(&other.val)
}
}