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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,6 @@ In jq, division by 0 yields an error, whereas
in jaq, `n / 0` yields `nan` if `n == 0`, `infinite` if `n > 0`, and `-infinite` if `n < 0`.
jaq's behaviour is closer to the IEEE standard for floating-point arithmetic (IEEE 754).

jaq implements a total ordering on floating-point numbers to allow sorting values.
Therefore, it unfortunately has to enforce that `nan == nan`.
(jq gets around this by enforcing that `nan < nan` is true, yet `nan > nan` is false,
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).



## Assignments

Expand Down
7 changes: 7 additions & 0 deletions jaq-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,14 @@ fn float_eq(left: f64, right: f64) -> bool {

fn float_cmp(left: f64, right: f64) -> Ordering {
if left == 0. && right == 0. {
// consider negative and positive 0 as equal
Ordering::Equal
} 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)
Comment on lines +894 to 901

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.

}
Expand Down
2 changes: 1 addition & 1 deletion jaq-std/src/defs.jq
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def isobject: . >= {};
# Numbers
def nan: 0 / 0;
def infinite: 1 / 0;
def isnan: . == nan;
def isnan: . < nan and nan < .;
def isinfinite: . == infinite or . == -infinite;
def isfinite: isnumber and (isinfinite | not);
def isnormal: isnumber and ((. == 0 or isnan or isinfinite) | not);
Expand Down
4 changes: 2 additions & 2 deletions jaq-std/tests/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ yields!(tofromdate, "946684800 | todate | fromdate", 946684800);

yields!(
drem_nan,
"[drem(nan, 1; nan, 1)] == [nan, nan, nan, 0.0]",
"[drem(nan, 1; nan, 1)] | (.[0:-1] | all(isnan)) and .[-1] == 0.0",
true
);
yields!(
Expand Down Expand Up @@ -237,7 +237,7 @@ yields!(
);
yields!(
scalb_nan,
"[scalb(nan, 1; nan, 1)] == [nan, nan, nan, 2.0]",
"[scalb(nan, 1; nan, 1)] | (.[0:-1] | all(isnan)) and .[-1] == 2.0",
true
);
yields!(
Expand Down
Loading