Skip to content

Commit

Permalink
Make reminder be integer reminder
Browse files Browse the repository at this point in the history
```
$ 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 13, 2024
1 parent e42d884 commit 28d9d72
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(Float(f % i as f64)),
(Int(i), Float(f)) if f != 0.0 => Ok(Float(i as f64 % f)),
(Float(x), Float(y)) if y != 0.0 => Ok(Float(x % y )),
(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",
-2.0,
-2,
-2,
-1,
0,
"cannot calculate -1 % 0",
-1.0,
-1,
-1,
0,
0,
"cannot calculate 0 % 0",
0.0,
0,
0,
0.10000000000000009,
0.10000000000000009,
"cannot calculate 2.1 % 0",
0.0,
2.1,
2.1,
1,
0,
"cannot calculate 3 % 0",
0.8999999999999999,
0,
3,
1,
0,
"cannot calculate 4000000001 % 0",
0.49999983082315813,
2,
0
])
);

0 comments on commit 28d9d72

Please sign in to comment.