Skip to content

Commit

Permalink
Introduce Num32 and Num64 types
Browse files Browse the repository at this point in the history
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
d-e-s-o committed Mar 19, 2022
1 parent c22a96e commit 960e506
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Unreleased
----------
- Introduced `Num32` and `Num64` types with fixed size and limited
precision


0.2.4
-----
- Added `num-v04` feature for using `num-*` crates in version `0.4`
Expand All @@ -8,7 +14,7 @@

0.2.3
-----
- Added efficient serialization & deserializtion support for non
- Added efficient serialization & deserialization support for non
self-describing formats such as `bincode`


Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
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)]
Expand Down Expand Up @@ -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;
49 changes: 44 additions & 5 deletions src/num.rs
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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

0 comments on commit 960e506

Please sign in to comment.