Implement the 2-number math functions for MathNumber.

master
Tom Alexander 4 years ago
parent 0a2e1b007c
commit f783f45d24
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

@ -52,7 +52,21 @@ impl Sub<MathNumber> for MathNumber {
type Output = Option<OwnedLiteral>;
fn sub(self, other: MathNumber) -> Self::Output {
todo!()
match (self, other) {
(MathNumber::Failure, _) | (_, MathNumber::Failure) => None,
(MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => {
math_ints(self_num, other_num, std::ops::Sub::sub)
}
(MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat(self_num - other_num))
}
(MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat((self_num as f64) - other_num))
}
(MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => {
Some(OwnedLiteral::LFloat(self_num - (other_num as f64)))
}
}
}
}
@ -60,7 +74,21 @@ impl Mul<MathNumber> for MathNumber {
type Output = Option<OwnedLiteral>;
fn mul(self, other: MathNumber) -> Self::Output {
todo!()
match (self, other) {
(MathNumber::Failure, _) | (_, MathNumber::Failure) => None,
(MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => {
math_ints(self_num, other_num, std::ops::Mul::mul)
}
(MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat(self_num * other_num))
}
(MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat((self_num as f64) * other_num))
}
(MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => {
Some(OwnedLiteral::LFloat(self_num * (other_num as f64)))
}
}
}
}
@ -68,7 +96,21 @@ impl Div<MathNumber> for MathNumber {
type Output = Option<OwnedLiteral>;
fn div(self, other: MathNumber) -> Self::Output {
todo!()
match (self, other) {
(MathNumber::Failure, _) | (_, MathNumber::Failure) => None,
(MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => {
Some(OwnedLiteral::LFloat((self_num as f64) / (other_num as f64)))
}
(MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat(self_num / other_num))
}
(MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat((self_num as f64) / other_num))
}
(MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => {
Some(OwnedLiteral::LFloat(self_num / (other_num as f64)))
}
}
}
}
@ -76,7 +118,21 @@ impl Rem<MathNumber> for MathNumber {
type Output = Option<OwnedLiteral>;
fn rem(self, other: MathNumber) -> Self::Output {
todo!()
match (self, other) {
(MathNumber::Failure, _) | (_, MathNumber::Failure) => None,
(MathNumber::Integer(self_num), MathNumber::Integer(other_num)) => {
math_ints(self_num, other_num, std::ops::Rem::rem)
}
(MathNumber::Decimal(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat(self_num % other_num))
}
(MathNumber::Integer(self_num), MathNumber::Decimal(other_num)) => {
Some(OwnedLiteral::LFloat((self_num as f64) % other_num))
}
(MathNumber::Decimal(self_num), MathNumber::Integer(other_num)) => {
Some(OwnedLiteral::LFloat(self_num % (other_num as f64)))
}
}
}
}

Loading…
Cancel
Save