Skip to content

Commit

Permalink
Removing approx partial eval
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed Jan 7, 2025
1 parent f75f79b commit 359a216
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions compiler/qsc_partial_eval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3169,12 +3169,20 @@ fn eval_bin_op_with_double_literals(
};

match bin_op {
BinOp::Eq => Ok(Value::Bool(f64_approx_eq(lhs, rhs))),
BinOp::Neq => Ok(Value::Bool(f64_approx_neq(lhs, rhs))),
BinOp::Eq => {
// matching simulator behavior
#[allow(clippy::float_cmp)]
Ok(Value::Bool(lhs == rhs))
}
BinOp::Neq => {
// matching simulator behavior
#[allow(clippy::float_cmp)]
Ok(Value::Bool(lhs != rhs))
}
BinOp::Gt => Ok(Value::Bool(lhs > rhs)),
BinOp::Gte => Ok(Value::Bool(f64_approx_gte(lhs, rhs))),
BinOp::Gte => Ok(Value::Bool(lhs >= rhs)),
BinOp::Lt => Ok(Value::Bool(lhs < rhs)),
BinOp::Lte => Ok(Value::Bool(f64_approx_lte(lhs, rhs))),
BinOp::Lte => Ok(Value::Bool(lhs <= rhs)),
BinOp::Add => Ok(Value::Double(lhs + rhs)),
BinOp::Sub => Ok(Value::Double(lhs - rhs)),
BinOp::Mul => Ok(Value::Double(lhs * rhs)),
Expand Down Expand Up @@ -3329,24 +3337,3 @@ fn try_get_eval_var_type(value: &Value) -> Option<VarTy> {
_ => None,
}
}

fn f64_approx_lte(lhs: f64, rhs: f64) -> bool {
lhs < rhs || f64_approx_eq(lhs, rhs)
}

fn f64_approx_gte(lhs: f64, rhs: f64) -> bool {
lhs > rhs || f64_approx_eq(lhs, rhs)
}

fn f64_approx_neq(lhs: f64, rhs: f64) -> bool {
!f64_approx_eq(lhs, rhs)
}

fn f64_approx_eq(lhs: f64, rhs: f64) -> bool {
// check equality, this is fastest and handles +0.0 == -0.0
// also lets us avoid constly math operations in the true case
lhs == rhs || {
// not equal, check if they are close enough
(lhs - rhs).abs() <= f64::EPSILON
}
}

0 comments on commit 359a216

Please sign in to comment.