-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change introduces the Num32 and Num64 types. Similar to the existing Num, they represent rational numbers. Unlike Num, they use fixed size integers for representing the numerator and denominator (namely each using two 32 or 64 bit integers, respectively). That means that no heap allocations occur when working with these and that we can estimate memory requirements. However, these types cannot provide unlimited precision as Num does.
- Loading branch information
Showing
3 changed files
with
54 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (C) 2019-2021 Daniel Mueller <[email protected]> | ||
// Copyright (C) 2019-2022 Daniel Mueller <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
#![allow(clippy::unreadable_literal)] | ||
|
@@ -66,4 +66,6 @@ mod ser; | |
|
||
pub use crate::num::CustomDisplay; | ||
pub use crate::num::Num; | ||
pub use crate::num::Num32; | ||
pub use crate::num::Num64; | ||
pub use crate::num::ParseNumError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// Copyright (C) 2019-2021 Daniel Mueller <[email protected]> | ||
// Copyright (C) 2019-2022 Daniel Mueller <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use std::error::Error as StdError; | ||
|
@@ -30,6 +30,8 @@ use crate::num_bigint::BigInt; | |
use crate::num_bigint::ParseBigIntError; | ||
use crate::num_bigint::Sign; | ||
use crate::num_rational::BigRational; | ||
use crate::num_rational::Rational32; | ||
use crate::num_rational::Rational64; | ||
|
||
|
||
/// The maximum precision we use when converting a `Num` into a string. | ||
|
@@ -178,10 +180,20 @@ impl<'n> Display for CustomDisplay<'n> { | |
} | ||
|
||
|
||
/// An unlimited precision number type with some improvements and | ||
/// customizations over `BigRational`. | ||
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct Num(pub(crate) BigRational); | ||
macro_rules! impl_num { | ||
($(#[$meta:meta])* pub struct $name:ident($rational:ty)) => { | ||
$(#[$meta])* | ||
pub struct $name(pub(crate) $rational); | ||
}; | ||
} | ||
|
||
|
||
impl_num! { | ||
/// An unlimited precision number type with some improvements and | ||
/// customizations over [`BigRational`]. | ||
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct Num(BigRational) | ||
} | ||
|
||
impl Num { | ||
/// Construct a `Num` from two integers. | ||
|
@@ -524,3 +536,30 @@ impl_assign_ops!(impl SubAssign, sub_assign); | |
impl_assign_ops!(impl MulAssign, mul_assign); | ||
impl_assign_ops!(impl DivAssign, div_assign); | ||
impl_assign_ops!(impl RemAssign, rem_assign); | ||
|
||
|
||
impl_num! { | ||
/// A fixed size number type with some improvements and customizations | ||
/// over [`Rational32`]. | ||
/// | ||
/// Please note that this type is meant to be used mostly in scenarios | ||
/// where memory boundedness is of paramount importance. Importantly, | ||
/// it does *not* constitute a fully blown replacement for [`Num`], as | ||
/// the provided functionality is much more limited (and likely will | ||
/// never catch up completely). | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct Num32(Rational32) | ||
} | ||
|
||
impl_num! { | ||
/// A fixed size number type with some improvements and customizations | ||
/// over [`Rational64`]. | ||
/// | ||
/// Please note that this type is meant to be used mostly in scenarios | ||
/// where memory boundedness is of paramount importance. Importantly, | ||
/// it does *not* constitute a fully blown replacement for [`Num`], as | ||
/// the provided functionality is much more limited (and likely will | ||
/// never catch up completely). | ||
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
pub struct Num64(Rational64) | ||
} |