Skip to content

Commit

Permalink
seq:update the digit counting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-sh committed Dec 28, 2024
1 parent a1315b3 commit 29601c8
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/uu/seq/src/floatparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ pub fn parse_hexadecimal_float(s: &str) -> Result<PreciseNumber, ParseNumberErro

// Build a PreciseNumber
let number = BigDecimal::from_f64(value).ok_or(ParseNumberError::Float)?;
let fractional_digits = i64::max(number.fractional_digit_count(), 0) as usize;
let fractional_digits = i64::max(number.fractional_digit_count(), 0);
let integral_digits =
number.digits() - fractional_digits as u64 + if sign < 0.0 { 1 } else { 0 };
Ok(PreciseNumber::new(
ExtendedBigDecimal::BigDecimal(number),
0,
fractional_digits,
integral_digits as usize,
fractional_digits as usize,
))
}

Expand Down Expand Up @@ -159,15 +161,22 @@ mod tests {

use super::parse_hexadecimal_float;
use crate::{numberparse::ParseNumberError, ExtendedBigDecimal};
use bigdecimal::BigDecimal;
use num_traits::ToPrimitive;

fn parse_f64(s: &str) -> Result<f64, ParseNumberError> {
fn parse_big_decimal(s: &str) -> Result<BigDecimal, ParseNumberError> {
match parse_hexadecimal_float(s)?.number {
ExtendedBigDecimal::BigDecimal(bd) => bd.to_f64().ok_or(ParseNumberError::Float),
ExtendedBigDecimal::BigDecimal(bd) => Ok(bd),
_ => Err(ParseNumberError::Float),
}
}

fn parse_f64(s: &str) -> Result<f64, ParseNumberError> {
parse_big_decimal(s)?
.to_f64()
.ok_or(ParseNumberError::Float)
}

#[test]
fn test_parse_precise_number_case_insensitive() {
assert_eq!(parse_f64("0x1P1").unwrap(), 2.0);
Expand Down Expand Up @@ -244,4 +253,23 @@ mod tests {
assert_eq!(parse_f64("0x1.8p2z").unwrap_err(), expected_error);
assert_eq!(parse_f64("0x1p3.2").unwrap_err(), expected_error);
}

#[test]
fn test_parse_precise_number_count_digits() {
let precise_num = parse_hexadecimal_float("0x1.2").unwrap(); // 1.125 decimal
assert_eq!(precise_num.num_integral_digits, 1);
assert_eq!(precise_num.num_fractional_digits, 3);

let precise_num = parse_hexadecimal_float("-0x1.2").unwrap(); // -1.125 decimal
assert_eq!(precise_num.num_integral_digits, 2);
assert_eq!(precise_num.num_fractional_digits, 3);

let precise_num = parse_hexadecimal_float("0x123.8").unwrap(); // 291.5 decimal
assert_eq!(precise_num.num_integral_digits, 3);
assert_eq!(precise_num.num_fractional_digits, 1);

let precise_num = parse_hexadecimal_float("-0x123.8").unwrap(); // -291.5 decimal
assert_eq!(precise_num.num_integral_digits, 4);
assert_eq!(precise_num.num_fractional_digits, 1);
}
}

0 comments on commit 29601c8

Please sign in to comment.