Skip to content

Commit

Permalink
Make reminder be integer reminder
Browse files Browse the repository at this point in the history
This is same as jq modulo (hoh) error message:
```
$ jq -n 'def f: -2, -1, 0, 2.1, 3, 4000000001; [f as $a | f as $b | try ($a % $b) catch .]'
[
  0,
  0,
  "number (-2) and number (0) cannot be divided (remainder) because the divisor is zero",
  0,
  -2,
  -2,
  -1,
  0,
  "number (-1) and number (0) cannot be divided (remainder) because the divisor is zero",
  -1,
  -1,
  -1,
  0,
  0,
  "number (0) and number (0) cannot be divided (remainder) because the divisor is zero",
  0,
  0,
  0,
  0,
  0,
  "number (2.1) and number (0) cannot be divided (remainder) because the divisor is zero",
  0,
  2,
  2,
  1,
  0,
  "number (3) and number (0) cannot be divided (remainder) because the divisor is zero",
  1,
  0,
  3,
  1,
  0,
  "number (4000000001) and number (0) cannot be divided (remainder) because the divisor is zero",
  1,
  2,
  0
]
```
  • Loading branch information
wader committed Nov 12, 2024
1 parent 98d6f35 commit 75135e5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
7 changes: 6 additions & 1 deletion jaq-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,9 +765,14 @@ impl core::ops::Div for Val {
impl core::ops::Rem for Val {
type Output = ValR;
fn rem(self, rhs: Self) -> Self::Output {
use Val::Int;
use Val::{Float, Int, Num};
match (self, rhs) {
(Int(x), Int(y)) if y != 0 => Ok(Int(x % y)),
(Float(f), Int(i)) if i != 0 => Ok(Int(f as isize % i)),
(Int(i), Float(f)) if f != 0.0 => Ok(Int(i % f as isize)),
(Float(x), Float(y)) if y != 0.0 => Ok(Int(x as isize % y as isize)),
(Num(n), r) => Self::from_dec_str(&n) % r,
(l, Num(n)) => l % Self::from_dec_str(&n),
(l, r) => Err(Error::math(l, ops::Math::Rem, r)),
}
}
Expand Down
43 changes: 43 additions & 0 deletions jaq-json/tests/funs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,46 @@ fn tojson() {
give(json!(0), "0.0 / 0.0 | tojson", json!("null"));
give(json!(0), "1.0 / 0.0 | tojson", json!("null"));
}

yields!(
math_rem,
"def f: -2, -1, 0, 2.1, 3, 4000000001; [f as $a | f as $b | try ($a % $b) catch .]",
json!([
0,
0,
"cannot calculate -2 % 0",
0,
-2,
-2,
-1,
0,
"cannot calculate -1 % 0",
-1,
-1,
-1,
0,
0,
"cannot calculate 0 % 0",
0,
0,
0,
0,
0,
"cannot calculate 2.1 % 0",
0,
2,
2,
1,
0,
"cannot calculate 3 % 0",
1,
0,
3,
1,
0,
"cannot calculate 4000000001 % 0",
1,
2,
0
])
);

0 comments on commit 75135e5

Please sign in to comment.