1
0

Merge branch 'main' into gui

This commit is contained in:
Jeff 2023-12-31 15:31:16 -05:00
commit 0bec02344e

View File

@ -313,18 +313,14 @@ impl Mul for Value {
type Output = Result<Self>; type Output = Result<Self>;
fn mul(self, other: Self) -> Self::Output { fn mul(self, other: Self) -> Self::Output {
if self.is_integer() && other.is_integer() { if let (Ok(left), Ok(right)) = (self.as_integer(), other.as_integer()) {
let left = self.as_integer().unwrap(); Ok(Value::Integer(left.saturating_mul(right)))
let right = other.as_integer().unwrap(); } else if let (Ok(left), Ok(right)) = (self.as_number(), other.as_number()) {
let value = Value::Integer(left.saturating_mul(right)); Ok(Value::Float(left * right))
Ok(value)
} else { } else {
let left = self.as_number()?; let non_number = if !self.is_number() { self } else { other };
let right = other.as_number()?;
let value = Value::Float(left * right);
Ok(value) Err(Error::ExpectedNumber { actual: non_number })
} }
} }
} }
@ -333,16 +329,20 @@ impl Div for Value {
type Output = Result<Self>; type Output = Result<Self>;
fn div(self, other: Self) -> Self::Output { fn div(self, other: Self) -> Self::Output {
let left = self.as_number()?; if let (Ok(left), Ok(right)) = (self.as_number(), other.as_number()) {
let right = other.as_number()?; let divided = left / right;
let result = left / right; let is_even = divided % 2.0 == 0.0;
let value = if result % 2.0 == 0.0 {
Value::Integer(result as i64)
} else {
Value::Float(result)
};
Ok(value) if self.is_integer() && other.is_integer() && is_even {
Ok(Value::Integer(divided as i64))
} else {
Ok(Value::Float(divided))
}
} else {
let non_number = if !self.is_number() { self } else { other };
Err(Error::ExpectedNumber { actual: non_number })
}
} }
} }