Skip to content

Commit 75135e5

Browse files
committed
Make reminder be integer reminder
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 ] ```
1 parent 98d6f35 commit 75135e5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

jaq-json/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,14 @@ impl core::ops::Div for Val {
765765
impl core::ops::Rem for Val {
766766
type Output = ValR;
767767
fn rem(self, rhs: Self) -> Self::Output {
768-
use Val::Int;
768+
use Val::{Float, Int, Num};
769769
match (self, rhs) {
770770
(Int(x), Int(y)) if y != 0 => Ok(Int(x % y)),
771+
(Float(f), Int(i)) if i != 0 => Ok(Int(f as isize % i)),
772+
(Int(i), Float(f)) if f != 0.0 => Ok(Int(i % f as isize)),
773+
(Float(x), Float(y)) if y != 0.0 => Ok(Int(x as isize % y as isize)),
774+
(Num(n), r) => Self::from_dec_str(&n) % r,
775+
(l, Num(n)) => l % Self::from_dec_str(&n),
771776
(l, r) => Err(Error::math(l, ops::Math::Rem, r)),
772777
}
773778
}

jaq-json/tests/funs.rs

+43
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,46 @@ fn tojson() {
7777
give(json!(0), "0.0 / 0.0 | tojson", json!("null"));
7878
give(json!(0), "1.0 / 0.0 | tojson", json!("null"));
7979
}
80+
81+
yields!(
82+
math_rem,
83+
"def f: -2, -1, 0, 2.1, 3, 4000000001; [f as $a | f as $b | try ($a % $b) catch .]",
84+
json!([
85+
0,
86+
0,
87+
"cannot calculate -2 % 0",
88+
0,
89+
-2,
90+
-2,
91+
-1,
92+
0,
93+
"cannot calculate -1 % 0",
94+
-1,
95+
-1,
96+
-1,
97+
0,
98+
0,
99+
"cannot calculate 0 % 0",
100+
0,
101+
0,
102+
0,
103+
0,
104+
0,
105+
"cannot calculate 2.1 % 0",
106+
0,
107+
2,
108+
2,
109+
1,
110+
0,
111+
"cannot calculate 3 % 0",
112+
1,
113+
0,
114+
3,
115+
1,
116+
0,
117+
"cannot calculate 4000000001 % 0",
118+
1,
119+
2,
120+
0
121+
])
122+
);

0 commit comments

Comments
 (0)