From 387098fc87c5301aeba3f64749322de9bbef1b0d Mon Sep 17 00:00:00 2001 From: Luccas Mateus Date: Wed, 31 Mar 2021 03:01:39 -0300 Subject: [PATCH] Stop nu panicks in math.round on a large decimal value(Most of the time) (#3224) * Stop crashing when dealing with large numbers in math round * Fix formatting * add tests * just to trigger wasm build * trigger wasm build --- crates/nu-command/src/commands/math/round.rs | 7 +++++-- crates/nu-command/tests/commands/math/mod.rs | 1 + .../nu-command/tests/commands/math/round.rs | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 crates/nu-command/tests/commands/math/round.rs diff --git a/crates/nu-command/src/commands/math/round.rs b/crates/nu-command/src/commands/math/round.rs index 33000fcbe8..0c36888d8c 100644 --- a/crates/nu-command/src/commands/math/round.rs +++ b/crates/nu-command/src/commands/math/round.rs @@ -76,9 +76,12 @@ fn round_big_int(val: BigInt) -> Value { fn round_big_decimal(val: BigDecimal, precision: i64) -> Value { if precision > 0 { - UntaggedValue::decimal(val.round(precision)).into() + UntaggedValue::decimal(val.with_scale(precision + 1).round(precision)).into() } else { - let (rounded, _) = val.round(precision).as_bigint_and_exponent(); + let (rounded, _) = val + .with_scale(precision + 1) + .round(precision) + .as_bigint_and_exponent(); UntaggedValue::int(rounded).into() } } diff --git a/crates/nu-command/tests/commands/math/mod.rs b/crates/nu-command/tests/commands/math/mod.rs index d95769174a..3bbcc3ea17 100644 --- a/crates/nu-command/tests/commands/math/mod.rs +++ b/crates/nu-command/tests/commands/math/mod.rs @@ -1,6 +1,7 @@ mod avg; mod eval; mod median; +mod round; mod sum; use nu_test_support::{nu, pipeline}; diff --git a/crates/nu-command/tests/commands/math/round.rs b/crates/nu-command/tests/commands/math/round.rs new file mode 100644 index 0000000000..2bec436a64 --- /dev/null +++ b/crates/nu-command/tests/commands/math/round.rs @@ -0,0 +1,21 @@ +use nu_test_support::nu; + +#[test] +fn can_round_very_large_numbers() { + let actual = nu!( + cwd: ".", + "echo 18.1372544780074142289927665486772012345 | math round" + ); + + assert_eq!(actual.out, "18") +} + +#[test] +fn can_round_very_large_numbers_with_precision() { + let actual = nu!( + cwd: ".", + "echo 18.13725447800741422899276654867720121457878988 | math round -p 10" + ); + + assert_eq!(actual.out, "18.137254478") +}