Skip to content

Commit

Permalink
optimize 0 special cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vic1707 committed Oct 31, 2023
1 parent a6ebf2b commit 217657b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/element/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ impl<'a> Simplify<'a> for BinOp<'a> {
rhs,
} if rhs == Number(1.0_f64) => lhs,
/////////////////////////// Divisions ///////////////////////////
// 0/0 => NaN // special case
BinOp {
op: Divide,
lhs,
rhs,
} if lhs == Number(0.0_f64) && rhs == Number(0.0_f64) => {
Number(f64::NAN)
},
// 0 / .. => 0
BinOp {
op: Divide, lhs, ..
Expand All @@ -115,6 +123,14 @@ impl<'a> Simplify<'a> for BinOp<'a> {
rhs,
} if lhs == rhs => Number(1.0_f64),
///////////////////////////// Powers ////////////////////////////
// 0 ^ 0 => 1 // special case
BinOp {
op: Power,
lhs,
rhs,
} if lhs == Number(0.0_f64) && rhs == Number(0.0_f64) => {
Number(1.0_f64)
},
// 0 ^ .. => 0
BinOp { op: Power, lhs, .. } if lhs == Number(0.0_f64) => {
Number(0.0_f64)
Expand All @@ -130,6 +146,14 @@ impl<'a> Simplify<'a> for BinOp<'a> {
rhs,
} if rhs == Number(1.0_f64) => lhs,
//////////////////////////// Modulos ////////////////////////////
// 0 % 0 => NaN // special case
BinOp {
op: Modulo,
lhs,
rhs,
} if lhs == Number(0.0_f64) && rhs == Number(0.0_f64) => {
Number(f64::NAN)
},
// 0 % .. => 0
BinOp {
op: Modulo, lhs, ..
Expand Down

0 comments on commit 217657b

Please sign in to comment.