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

NaN compatibility #248

Merged
merged 3 commits into from
Dec 19, 2024
Merged

NaN compatibility #248

merged 3 commits into from
Dec 19, 2024

Conversation

01mf02
Copy link
Owner

@01mf02 01mf02 commented Dec 19, 2024

This aligns jaq's and jq's handling of NaN (not a number), by enforcing nan < nan.

Before this change, it could happen that operations that yielded nan could have introduced non-deterministic behaviour, see #243. With this PR, the non-deterministic floating-point behaviour is still there, but it should not be observable anymore.

which breaks basic laws about total orders.)

Like jq, jaq prints `nan` and `infinite` as `null` in JSON,
because JSON does not support encoding these values as numbers.
Copy link
Contributor

Choose a reason for hiding this comment

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

Was intentional to remove the part about null as JSON?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, because that section is about differences wrt jq, not about resemblances. In the same spirit, I have already removed other sections that once talked about a difference, but later became obsolete (in particular the section on recursive functions).

@01mf02 01mf02 merged commit e3985c1 into main Dec 19, 2024
1 check passed
@01mf02
Copy link
Owner Author

01mf02 commented Dec 19, 2024

Thanks for having had a look at this, @wader!

@01mf02 01mf02 deleted the nan-compat branch December 19, 2024 17:21
Comment on lines +894 to 901
} else if left.is_nan() && right.is_nan() {
// there are more than 50 shades of NaN, and which of these
// you strike when you perform a calculation is not deterministic (!),
// therefore `total_cmp` may yield different results for the same calculation
// so we bite the bullet and handle this like in jq
Ordering::Less
} else {
f64::total_cmp(&left, &right)

Choose a reason for hiding this comment

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

This code still allows for -NaN and +NaN comparison nondeterminism when one of the inputs is non-NaN.

01mf02 added a commit that referenced this pull request Dec 23, 2024
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.

3 participants