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

Add reminder op for floats #229

Merged
merged 1 commit into from
Nov 25, 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
7 changes: 6 additions & 1 deletion jaq-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,9 +772,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)) => Ok(Float(f % i as f64)),
(Int(i), Float(f)) => Ok(Float(i as f64 % f)),
(Float(x), Float(y)) => Ok(Float(x % y)),
(Num(n), r) => Self::from_dec_str(&n) % r,
(l, Num(n)) => l % Self::from_dec_str(&n),
wader marked this conversation as resolved.
Show resolved Hide resolved
(l, r) => Err(Error::math(l, ops::Math::Rem, r)),
}
}
Expand Down
47 changes: 47 additions & 0 deletions jaq-json/tests/funs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,50 @@ fn tojson() {
give(json!(0), "0.0 / 0.0 | tojson", json!("null"));
give(json!(0), "1.0 / 0.0 | tojson", json!("null"));
}

#[test]
fn math_rem() {
// generated with this command with modification for errors and float rounding
// cargo run -- -rn 'def f: -2, -1, 0, 2.1, 3, 4000000001; f as $a | f as $b | "give!(json!(null), \"\($a) / \($b)\", \(try ($a % $b) catch tojson));"'
// TODO: use fail!()?
wader marked this conversation as resolved.
Show resolved Hide resolved
give(json!(null), "-2 % -2", json!(0));
give(json!(null), "-2 % -1", json!(0));
give(json!(null), "try (-2 % 0) catch .", json!("cannot calculate -2 % 0"));
give(json!(null), "-2 % 2.1", json!(-2.0));
give(json!(null), "-2 % 3", json!(-2));
give(json!(null), "-2 % 4000000001", json!(-2));
give(json!(null), "-1 % -2", json!(-1));
give(json!(null), "-1 % -1", json!(0));
give(json!(null), "try (-1 % 0) catch .", json!("cannot calculate -1 % 0"));
give(json!(null), "-1 % 2.1", json!(-1.0));
give(json!(null), "-1 % 3", json!(-1));
give(json!(null), "-1 % 4000000001", json!(-1));
give(json!(null), "0 % -2", json!(0));
give(json!(null), "0 % -1", json!(0));
give(json!(null), "try (0 % 0) catch .", json!("cannot calculate 0 % 0"));
give(json!(null), "0 % 2.1", json!(0.0));
give(json!(null), "0 % 3", json!(0));
give(json!(null), "0 % 4000000001", json!(0));
give(json!(null), "2.1 % -2 | . * 1000 | round", json!(100));
give(json!(null), "2.1 % -1 | . * 1000 | round", json!(100));
give(json!(null), "2.1 % 0 | isnan", json!(true));
give(json!(null), "2.1 % 2.1", json!(0.0));
give(json!(null), "2.1 % 3", json!(2.1));
give(json!(null), "2.1 % 4000000001", json!(2.1));
give(json!(null), "3 % -2", json!(1));
give(json!(null), "3 % -1", json!(0));
give(json!(null), "try (3 % 0) catch .", json!("cannot calculate 3 % 0"));
give(json!(null), "3 % 2.1 | . * 1000 | round", json!(900));
give(json!(null), "3 % 3", json!(0));
give(json!(null), "3 % 4000000001", json!(3));
give(json!(null), "4000000001 % -2", json!(1));
give(json!(null), "4000000001 % -1", json!(0));
give(
json!(null),
"try (4000000001 % 0) catch .",
json!("cannot calculate 4000000001 % 0"),
);
give(json!(null), "4000000001 % 2.1 | . * 1000 | round", json!(500));
give(json!(null), "4000000001 % 3", json!(2));
give(json!(null), "4000000001 % 4000000001", json!(0));
}
Loading