Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NaN comparison #647

Merged
merged 3 commits into from
Nov 8, 2024
Merged

Fixed NaN comparison #647

merged 3 commits into from
Nov 8, 2024

Conversation

rben01
Copy link
Contributor

@rben01 rben01 commented Nov 3, 2024

All comparisons involving at least one NaN now return false, regardless of units

Closes #644

All comparisons involving at least one NaN now return false, regardless of units
@rben01
Copy link
Contributor Author

rben01 commented Nov 3, 2024

One question is whether NaN should be comparable with other quantities regardless of units. I think yes, it is like 0 in this regard: the answer is known to be false regardless of units.

Comment on lines 348 to 354
pub(crate) enum QuantityOrdering {
IncompatibleUnits,
NanOperand,
Less,
Equal,
Greater,
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this lead to slightly less code?

Suggested change
pub(crate) enum QuantityOrdering {
IncompatibleUnits,
NanOperand,
Less,
Equal,
Greater,
}
pub(crate) enum QuantityOrderingResult {
IncompatibleUnits,
NanOperand,
Ok(std::cmp::Ordering),
}

std::cmp::Ordering::Equal => QuantityOrdering::Equal,
std::cmp::Ordering::Greater => QuantityOrdering::Greater,
},
None => unreachable!("unexpectedly got a None partial_cmp from non-NaN arguments"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe catch this with a .expect(…) above, using the same message?

@sharkdp
Copy link
Owner

sharkdp commented Nov 3, 2024

One question is whether NaN should be comparable with other quantities regardless of units. I think yes, it is like 0 in this regard: the answer is known to be false regardless of units.

Yes, we want NaN to be polymorphic. If 0 is polymorphic, then so is 0 × inf == NaN:

meter × NaN = meter × (0 × inf) = (meter × 0) × inf = 0 × inf = NaN

(ignoring associativity-violations in FP math for a second)

If NaN wouldn't be polymorphic, that equation above would not by typeable (Length × Scalar = Scalar?).

@rben01 rben01 reopened this Nov 4, 2024
@rben01
Copy link
Contributor Author

rben01 commented Nov 4, 2024

Not sure how I accidentally closed this or why I ended up force pushing, or why the macOS job didn't complete, but this PR should be good now.

Copy link
Owner

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@sharkdp sharkdp merged commit 1eb15a6 into sharkdp:master Nov 8, 2024
28 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NaN < 0 errors
2 participants