diff --git a/jaq-json/src/lib.rs b/jaq-json/src/lib.rs index 6acaeb402..18297e9d6 100644 --- a/jaq-json/src/lib.rs +++ b/jaq-json/src/lib.rs @@ -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)), } } diff --git a/jaq-json/tests/funs.rs b/jaq-json/tests/funs.rs index bf94ad901..feffc00fa 100644 --- a/jaq-json/tests/funs.rs +++ b/jaq-json/tests/funs.rs @@ -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 + ]) +);