Skip to content

Commit af492c7

Browse files
committed
seq: lots of changes (WIP)
1 parent 6481d63 commit af492c7

File tree

8 files changed

+104
-569
lines changed

8 files changed

+104
-569
lines changed

src/uu/seq/src/extendedbigdecimal.rs

+5-49
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ use std::fmt::Display;
2525
use std::ops::Add;
2626

2727
use bigdecimal::BigDecimal;
28-
use num_bigint::BigInt;
29-
use num_bigint::ToBigInt;
30-
use num_traits::One;
3128
use num_traits::Zero;
3229

33-
use crate::extendedbigint::ExtendedBigInt;
34-
3530
#[derive(Debug, Clone)]
3631
pub enum ExtendedBigDecimal {
3732
/// Arbitrary precision floating point number.
@@ -72,53 +67,14 @@ pub enum ExtendedBigDecimal {
7267
Nan,
7368
}
7469

75-
/// The smallest integer greater than or equal to this number.
76-
fn ceil(x: BigDecimal) -> BigInt {
77-
if x.is_integer() {
78-
// Unwrapping the Option because it always returns Some
79-
x.to_bigint().unwrap()
80-
} else {
81-
(x + BigDecimal::one().half()).round(0).to_bigint().unwrap()
82-
}
83-
}
84-
85-
/// The largest integer less than or equal to this number.
86-
fn floor(x: BigDecimal) -> BigInt {
87-
if x.is_integer() {
88-
// Unwrapping the Option because it always returns Some
89-
x.to_bigint().unwrap()
90-
} else {
91-
(x - BigDecimal::one().half()).round(0).to_bigint().unwrap()
92-
}
93-
}
94-
9570
impl ExtendedBigDecimal {
96-
/// The smallest integer greater than or equal to this number.
97-
pub fn ceil(self) -> ExtendedBigInt {
98-
match self {
99-
Self::BigDecimal(x) => ExtendedBigInt::BigInt(ceil(x)),
100-
other => From::from(other),
101-
}
71+
#[cfg(test)]
72+
pub fn zero() -> Self {
73+
Self::BigDecimal(1.into())
10274
}
10375

104-
/// The largest integer less than or equal to this number.
105-
pub fn floor(self) -> ExtendedBigInt {
106-
match self {
107-
Self::BigDecimal(x) => ExtendedBigInt::BigInt(floor(x)),
108-
other => From::from(other),
109-
}
110-
}
111-
}
112-
113-
impl From<ExtendedBigInt> for ExtendedBigDecimal {
114-
fn from(big_int: ExtendedBigInt) -> Self {
115-
match big_int {
116-
ExtendedBigInt::BigInt(n) => Self::BigDecimal(BigDecimal::from(n)),
117-
ExtendedBigInt::Infinity => Self::Infinity,
118-
ExtendedBigInt::MinusInfinity => Self::MinusInfinity,
119-
ExtendedBigInt::MinusZero => Self::MinusZero,
120-
ExtendedBigInt::Nan => Self::Nan,
121-
}
76+
pub fn one() -> Self {
77+
Self::BigDecimal(1.into())
12278
}
12379
}
12480

src/uu/seq/src/extendedbigint.rs

-214
This file was deleted.

src/uu/seq/src/number.rs

+3-67
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,6 @@
1212
use num_traits::Zero;
1313

1414
use crate::extendedbigdecimal::ExtendedBigDecimal;
15-
use crate::extendedbigint::ExtendedBigInt;
16-
17-
/// An integral or floating point number.
18-
#[derive(Debug, PartialEq)]
19-
pub enum Number {
20-
Int(ExtendedBigInt),
21-
Float(ExtendedBigDecimal),
22-
}
23-
24-
impl Number {
25-
/// Decide whether this number is zero (either positive or negative).
26-
pub fn is_zero(&self) -> bool {
27-
// We would like to implement `num_traits::Zero`, but it
28-
// requires an addition implementation, and we don't want to
29-
// implement that here.
30-
match self {
31-
Self::Int(n) => n.is_zero(),
32-
Self::Float(x) => x.is_zero(),
33-
}
34-
}
35-
36-
/// Convert this number into an `ExtendedBigDecimal`.
37-
pub fn into_extended_big_decimal(self) -> ExtendedBigDecimal {
38-
match self {
39-
Self::Int(n) => ExtendedBigDecimal::from(n),
40-
Self::Float(x) => x,
41-
}
42-
}
43-
44-
/// The integer number one.
45-
pub fn one() -> Self {
46-
// We would like to implement `num_traits::One`, but it requires
47-
// a multiplication implementation, and we don't want to
48-
// implement that here.
49-
Self::Int(ExtendedBigInt::one())
50-
}
51-
52-
/// Round this number towards the given other number.
53-
///
54-
/// If `other` is greater, then round up. If `other` is smaller,
55-
/// then round down.
56-
pub fn round_towards(self, other: &ExtendedBigInt) -> ExtendedBigInt {
57-
match self {
58-
// If this number is already an integer, it is already
59-
// rounded to the nearest integer in the direction of
60-
// `other`.
61-
Self::Int(num) => num,
62-
// Otherwise, if this number is a float, we need to decide
63-
// whether `other` is larger or smaller than it, and thus
64-
// whether to round up or round down, respectively.
65-
Self::Float(num) => {
66-
let other: ExtendedBigDecimal = From::from(other.clone());
67-
if other > num {
68-
num.ceil()
69-
} else {
70-
// If they are equal, then `self` is already an
71-
// integer, so calling `floor()` does no harm and
72-
// will just return that integer anyway.
73-
num.floor()
74-
}
75-
}
76-
}
77-
}
78-
}
7915

8016
/// A number with a specified number of integer and fractional digits.
8117
///
@@ -87,13 +23,13 @@ impl Number {
8723
/// You can get an instance of this struct by calling [`str::parse`].
8824
#[derive(Debug)]
8925
pub struct PreciseNumber {
90-
pub number: Number,
26+
pub number: ExtendedBigDecimal,
9127
pub num_integral_digits: usize,
9228
pub num_fractional_digits: usize,
9329
}
9430

9531
impl PreciseNumber {
96-
pub fn new(number: Number, num_integral_digits: usize, num_fractional_digits: usize) -> Self {
32+
pub fn new(number: ExtendedBigDecimal, num_integral_digits: usize, num_fractional_digits: usize) -> Self {
9733
Self {
9834
number,
9935
num_integral_digits,
@@ -106,7 +42,7 @@ impl PreciseNumber {
10642
// We would like to implement `num_traits::One`, but it requires
10743
// a multiplication implementation, and we don't want to
10844
// implement that here.
109-
Self::new(Number::one(), 1, 0)
45+
Self::new(ExtendedBigDecimal::one(), 1, 0)
11046
}
11147

11248
/// Decide whether this number is zero (either positive or negative).

0 commit comments

Comments
 (0)