copr: fix float64 overflow check in plus/minus real function
@@ -102,12 +102,11 @@ impl ArithmeticOp for RealPlus {
type T = Real;
fn calc(lhs: &Real, rhs: &Real) -> Result<Option<Real>> {
- if (**lhs > 0f64 && **rhs > (std::f64::MAX - **lhs))
- || (**lhs < 0f64 && **rhs < (-std::f64::MAX - **lhs))
- {
+ let res = *lhs + *rhs;
+ if !res.is_finite() {
return Err(Error::overflow("DOUBLE", &format!("({} + {})", lhs, rhs)).into());
}
- Ok(Some(*lhs + *rhs))
+ Ok(Some(res))
}
}
@@ -195,7 +194,7 @@ impl ArithmeticOp for RealMinus {
fn calc(lhs: &Real, rhs: &Real) -> Result<Option<Real>> {
let res = *lhs - *rhs;
- if res.is_infinite() {
+ if !res.is_finite() {
return Err(Error::overflow("DOUBLE", &format!("({} - {})", lhs, rhs)).into());
}
Ok(Some(res))
@@ -571,8 +570,8 @@ mod tests {
(
Real::new(std::f64::MAX - 1f64).ok(),
Real::new(2f64).ok(),
- None,
- true,
+ Real::new(std::f64::MAX).ok(),
+ false,
),
];
for (lhs, rhs, expected, is_err) in test_cases {
@@ -681,6 +680,12 @@ mod tests {
None,
true,
),
+ (
+ Real::new(f64::MIN).ok(),
+ Real::new(1f64).ok(),
+ Real::new(f64::MIN).ok(),
+ false,
+ ),
];
for (lhs, rhs, expected, is_err) in test_cases {
let output = RpnFnScalarEvaluator::new()
