Skip to content

Commit

Permalink
fixup! seq: use BigDecimal to represent floats
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed Oct 6, 2021
1 parent 0e90c48 commit 621ffa7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/uu/seq/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 15 additions & 1 deletion src/uu/seq/src/numberparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,10 +123,15 @@ fn parse_exponent_no_decimal(s: &str, j: usize) -> Result<PreciseNumber, ParseNu
2
} else {
let total = j as i64 + exponent;
if total < 1 {
let result = if total < 1 {
1
} else {
total.try_into().unwrap()
};
if x.sign() == Sign::Minus {
result + 1
} else {
result
}
};
let num_fractional_digits = if exponent < 0 { -exponent as usize } else { 0 };
Expand Down Expand Up @@ -384,6 +390,12 @@ mod tests {
"1.0".parse::<BigDecimal>().unwrap()
))
);
assert_eq!(
parse("-1e-3"),
Number::Float(ExtendedBigDecimal::BigDecimal(
"-0.001".parse::<BigDecimal>().unwrap()
))
);
}

#[test]
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

0 comments on commit 621ffa7

Please sign in to comment.