Skip to content

Commit

Permalink
fix: make NaN == NaN inside Expression
Browse files Browse the repository at this point in the history
This is the easiest way to resolve #316 in a way that doesn't break the
API (the other way being to use `OrderedFloat`).
  • Loading branch information
Shadow53 committed Nov 23, 2023
1 parent eec9738 commit 39f4714
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions quil-rs/proptest-regressions/expression/mod.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 0c84b97c710f57935dd9b81feff3bd3c65662286d475e4d0c02c5b89c4911e9a # shrinks to e = Infix(InfixExpression { left: Address(MemoryReference { name: "aa", index: 0 }), operator: Slash, right: Infix(InfixExpression { left: PiConstant, operator: Minus, right: PiConstant }) })
13 changes: 12 additions & 1 deletion quil-rs/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ impl PartialEq for Expression {
match (self, other) {
(Self::Address(left), Self::Address(right)) => left == right,
(Self::Infix(left), Self::Infix(right)) => left == right,
(Self::Number(left), Self::Number(right)) => left == right,
(Self::Number(left), Self::Number(right)) => {
(left.re == right.re || left.re.is_nan() && right.re.is_nan()) && (left.im == right.im || left.im.is_nan() && right.im.is_nan())
},
(Self::Prefix(left), Self::Prefix(right)) => left == right,
(Self::FunctionCall(left), Self::FunctionCall(right)) => left == right,
(Self::Variable(left), Self::Variable(right)) => left == right,
Expand Down Expand Up @@ -990,6 +992,7 @@ mod tests {
prop_assert!(p.is_ok());
let p = p.unwrap();
let simple_p = p.clone().into_simplified();

prop_assert_eq!(
simple_p.clone(),
simple_e.clone(),
Expand Down Expand Up @@ -1018,13 +1021,21 @@ mod tests {
assert_eq!(input, &restring);
}
}

#[test]
fn test_nan_is_equal() {
let left = Expression::Number(f64::NAN.into());
let right = left.clone();
assert_eq!(left, right);
}

#[test]
fn specific_simplification_tests() {
for (input, expected) in vec![
("pi", Expression::Number(PI.into())),
("pi/2", Expression::Number((PI / 2.0).into())),
("pi * pi", Expression::Number((PI.powi(2)).into())),
("1.0/(1.0-1.0)", Expression::Number(f64::NAN.into())),
(
"(a[0]*2*pi)/6.283185307179586",
Expression::Address(MemoryReference {
Expand Down

0 comments on commit 39f4714

Please sign in to comment.