about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2024-12-09 09:25:22 +0000
committerTrevor Gross <tmgross@umich.edu>2025-03-02 07:08:01 +0000
commit626d2c5eed687e2c166d25f29cdfcaaba48f5f23 (patch)
tree258bffdc5d9014edfbe9b999ddcf76f79c70d423
parent49a2d4c757e27f915d15664ba92bbe0d3f590a8e (diff)
downloadrust-626d2c5eed687e2c166d25f29cdfcaaba48f5f23.tar.gz
rust-626d2c5eed687e2c166d25f29cdfcaaba48f5f23.zip
dec2flt: Rename `Number` to `Decimal`
The previous commit renamed `Decimal` to `DecimalSeq`. Now, rename the
type that represents a decimal floating point number to be `Decimal`.

Additionally, add some tests for internal behavior.
-rw-r--r--library/core/src/num/dec2flt/decimal.rs (renamed from library/core/src/num/dec2flt/number.rs)6
-rw-r--r--library/core/src/num/dec2flt/mod.rs2
-rw-r--r--library/core/src/num/dec2flt/parse.rs10
-rw-r--r--library/coretests/tests/num/dec2flt/decimal.rs28
-rw-r--r--library/coretests/tests/num/dec2flt/mod.rs1
-rw-r--r--library/coretests/tests/num/dec2flt/parse.rs26
6 files changed, 51 insertions, 22 deletions
diff --git a/library/core/src/num/dec2flt/number.rs b/library/core/src/num/dec2flt/decimal.rs
index 2538991564a..1ba9150a011 100644
--- a/library/core/src/num/dec2flt/number.rs
+++ b/library/core/src/num/dec2flt/decimal.rs
@@ -3,7 +3,6 @@
 use crate::num::dec2flt::float::RawFloat;
 use crate::num::dec2flt::fpu::set_precision;
 
-#[rustfmt::skip]
 const INT_POW10: [u64; 16] = [
     1,
     10,
@@ -23,15 +22,16 @@ const INT_POW10: [u64; 16] = [
     1000000000000000,
 ];
 
+/// A floating point number with up to 64 bits of mantissa and an `i64` exponent.
 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
-pub struct Number {
+pub struct Decimal {
     pub exponent: i64,
     pub mantissa: u64,
     pub negative: bool,
     pub many_digits: bool,
 }
 
-impl Number {
+impl Decimal {
     /// Detect if the float can be accurately reconstructed from native floats.
     #[inline]
     fn is_fast_path<F: RawFloat>(&self) -> bool {
diff --git a/library/core/src/num/dec2flt/mod.rs b/library/core/src/num/dec2flt/mod.rs
index 471505d2494..a82c858df49 100644
--- a/library/core/src/num/dec2flt/mod.rs
+++ b/library/core/src/num/dec2flt/mod.rs
@@ -97,6 +97,7 @@ use crate::fmt;
 use crate::str::FromStr;
 
 mod common;
+pub mod decimal;
 pub mod decimal_seq;
 mod fpu;
 mod slow;
@@ -104,7 +105,6 @@ mod table;
 // float is used in flt2dec, and all are used in unit tests.
 pub mod float;
 pub mod lemire;
-pub mod number;
 pub mod parse;
 
 macro_rules! from_str_float_impl {
diff --git a/library/core/src/num/dec2flt/parse.rs b/library/core/src/num/dec2flt/parse.rs
index 06ee8e95fbc..e38fedc58be 100644
--- a/library/core/src/num/dec2flt/parse.rs
+++ b/library/core/src/num/dec2flt/parse.rs
@@ -1,8 +1,8 @@
 //! Functions to parse floating-point numbers.
 
 use crate::num::dec2flt::common::{ByteSlice, is_8digits};
+use crate::num::dec2flt::decimal::Decimal;
 use crate::num::dec2flt::float::RawFloat;
-use crate::num::dec2flt::number::Number;
 
 const MIN_19DIGIT_INT: u64 = 100_0000_0000_0000_0000;
 
@@ -100,7 +100,7 @@ fn parse_scientific(s_ref: &mut &[u8]) -> Option<i64> {
 ///
 /// This creates a representation of the float as the
 /// significant digits and the decimal exponent.
-fn parse_partial_number(mut s: &[u8]) -> Option<(Number, usize)> {
+fn parse_partial_number(mut s: &[u8]) -> Option<(Decimal, usize)> {
     debug_assert!(!s.is_empty());
 
     // parse initial digits before dot
@@ -146,7 +146,7 @@ fn parse_partial_number(mut s: &[u8]) -> Option<(Number, usize)> {
 
     // handle uncommon case with many digits
     if n_digits <= 19 {
-        return Some((Number { exponent, mantissa, negative: false, many_digits: false }, len));
+        return Some((Decimal { exponent, mantissa, negative: false, many_digits: false }, len));
     }
 
     n_digits -= 19;
@@ -179,13 +179,13 @@ fn parse_partial_number(mut s: &[u8]) -> Option<(Number, usize)> {
         exponent += exp_number;
     }
 
-    Some((Number { exponent, mantissa, negative: false, many_digits }, len))
+    Some((Decimal { exponent, mantissa, negative: false, many_digits }, len))
 }
 
 /// Try to parse a non-special floating point number,
 /// as well as two slices with integer and fractional parts
 /// and the parsed exponent.
-pub fn parse_number(s: &[u8]) -> Option<Number> {
+pub fn parse_number(s: &[u8]) -> Option<Decimal> {
     if let Some((float, rest)) = parse_partial_number(s) {
         if rest == s.len() {
             return Some(float);
diff --git a/library/coretests/tests/num/dec2flt/decimal.rs b/library/coretests/tests/num/dec2flt/decimal.rs
new file mode 100644
index 00000000000..1fa06de692e
--- /dev/null
+++ b/library/coretests/tests/num/dec2flt/decimal.rs
@@ -0,0 +1,28 @@
+use core::num::dec2flt::decimal::Decimal;
+
+type FPath<F> = ((i64, u64, bool, bool), Option<F>);
+
+const FPATHS_F32: &[FPath<f32>] =
+    &[((0, 0, false, false), Some(0.0)), ((0, 0, false, false), Some(0.0))];
+const FPATHS_F64: &[FPath<f64>] =
+    &[((0, 0, false, false), Some(0.0)), ((0, 0, false, false), Some(0.0))];
+
+#[test]
+fn check_fast_path_f32() {
+    for ((exponent, mantissa, negative, many_digits), expected) in FPATHS_F32.iter().copied() {
+        let dec = Decimal { exponent, mantissa, negative, many_digits };
+        let actual = dec.try_fast_path::<f32>();
+
+        assert_eq!(actual, expected);
+    }
+}
+
+#[test]
+fn check_fast_path_f64() {
+    for ((exponent, mantissa, negative, many_digits), expected) in FPATHS_F64.iter().copied() {
+        let dec = Decimal { exponent, mantissa, negative, many_digits };
+        let actual = dec.try_fast_path::<f64>();
+
+        assert_eq!(actual, expected);
+    }
+}
diff --git a/library/coretests/tests/num/dec2flt/mod.rs b/library/coretests/tests/num/dec2flt/mod.rs
index 51f3017ddc7..a9025be5ca7 100644
--- a/library/coretests/tests/num/dec2flt/mod.rs
+++ b/library/coretests/tests/num/dec2flt/mod.rs
@@ -1,5 +1,6 @@
 #![allow(overflowing_literals)]
 
+mod decimal;
 mod decimal_seq;
 mod float;
 mod lemire;
diff --git a/library/coretests/tests/num/dec2flt/parse.rs b/library/coretests/tests/num/dec2flt/parse.rs
index 4a5d24ba7d5..ec705a61e13 100644
--- a/library/coretests/tests/num/dec2flt/parse.rs
+++ b/library/coretests/tests/num/dec2flt/parse.rs
@@ -1,9 +1,9 @@
-use core::num::dec2flt::number::Number;
+use core::num::dec2flt::decimal::Decimal;
 use core::num::dec2flt::parse::parse_number;
 use core::num::dec2flt::{dec2flt, pfe_invalid};
 
-fn new_number(e: i64, m: u64) -> Number {
-    Number { exponent: e, mantissa: m, negative: false, many_digits: false }
+fn new_dec(e: i64, m: u64) -> Decimal {
+    Decimal { exponent: e, mantissa: m, negative: false, many_digits: false }
 }
 
 #[test]
@@ -31,23 +31,23 @@ fn invalid_chars() {
     }
 }
 
-fn parse_positive(s: &[u8]) -> Option<Number> {
+fn parse_positive(s: &[u8]) -> Option<Decimal> {
     parse_number(s)
 }
 
 #[test]
 fn valid() {
-    assert_eq!(parse_positive(b"123.456e789"), Some(new_number(786, 123456)));
-    assert_eq!(parse_positive(b"123.456e+789"), Some(new_number(786, 123456)));
-    assert_eq!(parse_positive(b"123.456e-789"), Some(new_number(-792, 123456)));
-    assert_eq!(parse_positive(b".050"), Some(new_number(-3, 50)));
-    assert_eq!(parse_positive(b"999"), Some(new_number(0, 999)));
-    assert_eq!(parse_positive(b"1.e300"), Some(new_number(300, 1)));
-    assert_eq!(parse_positive(b".1e300"), Some(new_number(299, 1)));
-    assert_eq!(parse_positive(b"101e-33"), Some(new_number(-33, 101)));
+    assert_eq!(parse_positive(b"123.456e789"), Some(new_dec(786, 123456)));
+    assert_eq!(parse_positive(b"123.456e+789"), Some(new_dec(786, 123456)));
+    assert_eq!(parse_positive(b"123.456e-789"), Some(new_dec(-792, 123456)));
+    assert_eq!(parse_positive(b".050"), Some(new_dec(-3, 50)));
+    assert_eq!(parse_positive(b"999"), Some(new_dec(0, 999)));
+    assert_eq!(parse_positive(b"1.e300"), Some(new_dec(300, 1)));
+    assert_eq!(parse_positive(b".1e300"), Some(new_dec(299, 1)));
+    assert_eq!(parse_positive(b"101e-33"), Some(new_dec(-33, 101)));
     let zeros = "0".repeat(25);
     let s = format!("1.5e{zeros}");
-    assert_eq!(parse_positive(s.as_bytes()), Some(new_number(-1, 15)));
+    assert_eq!(parse_positive(s.as_bytes()), Some(new_dec(-1, 15)));
 }
 
 macro_rules! assert_float_result_bits_eq {