From 621ffa7805f2552467dab21c8b13a440ed890e14 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Tue, 5 Oct 2021 20:47:58 -0400 Subject: [PATCH] fixup! seq: use BigDecimal to represent floats --- src/uu/seq/src/number.rs | 5 +++-- src/uu/seq/src/numberparse.rs | 16 +++++++++++++++- tests/by-util/test_seq.rs | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/uu/seq/src/number.rs b/src/uu/seq/src/number.rs index df2f1d1b300..52b6fa94465 100644 --- a/src/uu/seq/src/number.rs +++ b/src/uu/seq/src/number.rs @@ -60,8 +60,9 @@ impl Number { /// A number with a specified number of integer and fractional digits. /// /// This struct can be used to represent a number along with information -/// on how many significant digits to use when displaying the -/// number. +/// on how many significant digits to use when displaying the number. +/// The [`num_integral_digits`] field also includes the width needed to +/// display the "-" character for a negative number. /// /// You can get an instance of this struct by calling [`str::parse`]. #[derive(Debug)] diff --git a/src/uu/seq/src/numberparse.rs b/src/uu/seq/src/numberparse.rs index 2f715f6557c..29952eba39b 100644 --- a/src/uu/seq/src/numberparse.rs +++ b/src/uu/seq/src/numberparse.rs @@ -8,6 +8,7 @@ use std::str::FromStr; use bigdecimal::BigDecimal; use num_bigint::BigInt; +use num_bigint::Sign; use num_traits::Zero; use crate::extendedbigdecimal::ExtendedBigDecimal; @@ -122,10 +123,15 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result().unwrap() )) ); + assert_eq!( + parse("-1e-3"), + Number::Float(ExtendedBigDecimal::BigDecimal( + "-0.001".parse::().unwrap() + )) + ); } #[test] @@ -449,6 +461,7 @@ mod tests { // exponent, no decimal assert_eq!(num_integral_digits("123e4"), 3 + 4); assert_eq!(num_integral_digits("123e-4"), 1); + assert_eq!(num_integral_digits("-1e-3"), 2); // decimal and exponent assert_eq!(num_integral_digits("123.45e6"), 3 + 6); assert_eq!(num_integral_digits("123.45e-6"), 1); @@ -475,6 +488,7 @@ mod tests { assert_eq!(num_fractional_digits("123e4"), 0); assert_eq!(num_fractional_digits("123e-4"), 4); assert_eq!(num_fractional_digits("123e-1"), 1); + assert_eq!(num_fractional_digits("-1e-3"), 3); // decimal and exponent assert_eq!(num_fractional_digits("123.45e6"), 0); assert_eq!(num_fractional_digits("123.45e1"), 1); diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 936cb1b79d9..94b80076690 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -416,6 +416,15 @@ fn test_width_decimal_scientific_notation_trailing_zeros_increment() { .no_stderr(); } +#[test] +fn test_width_negative_scientific_notation() { + new_ucmd!() + .args(&["-w", "-1e-3", "1"]) + .succeeds() + .stdout_is("-0.001\n00.999\n") + .no_stderr(); +} + /// Test that trailing zeros in the end argument do not contribute to width. #[test] fn test_width_decimal_scientific_notation_trailing_zeros_end() { @@ -523,3 +532,12 @@ fn test_negative_increment_decimal() { .stdout_is("0.1\n0.0\n-0.1\n-0.2\n") .no_stderr(); } + +#[test] +fn test_zero_not_first() { + new_ucmd!() + .args(&["-w", "-0.1", "0.1", "0.1"]) + .succeeds() + .stdout_is("-0.1\n00.0\n00.1\n") + .no_stderr(); +}