Skip to content

Commit

Permalink
add sqrt methods for GenericDecimal
Browse files Browse the repository at this point in the history
  • Loading branch information
squ1dd13 committed Sep 19, 2023
1 parent 345a32e commit 35c7871
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
60 changes: 60 additions & 0 deletions src/decimal/approx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::{
approx::sqrt::RawApprox, fraction::approx::Accuracy, generic::GenericInteger, GenericDecimal,
};
use num::{
bigint::{ToBigInt, ToBigUint},
BigUint, Integer,
};
use std::borrow::Borrow;

impl<
T: Clone + Integer + ToBigUint + ToBigInt + GenericInteger,
P: Copy + Integer + Into<usize>,
> GenericDecimal<T, P>
{
/// See `GenericFraction::sqrt_abs_with_accuracy_raw`.
pub fn sqrt_abs_with_accuracy_raw(&self, accuracy: impl Borrow<Accuracy>) -> RawApprox {
self.0.sqrt_abs_with_accuracy_raw(accuracy)
}

/// See `GenericFraction::sqrt_abs_with_accuracy`.
pub fn sqrt_abs_with_accuracy(
&self,
accuracy: impl Borrow<Accuracy>,
) -> GenericDecimal<BigUint, P> {
GenericDecimal(self.0.sqrt_abs_with_accuracy(accuracy), self.1)
}

/// See `GenericFraction::sqrt_abs_raw`.
pub fn sqrt_abs_raw(&self, decimal_places: u32) -> RawApprox {
self.0.sqrt_abs_raw(decimal_places)
}

/// See `GenericFraction::sqrt_abs`.
pub fn sqrt_abs(&self, decimal_places: u32) -> GenericDecimal<BigUint, P> {
GenericDecimal(self.0.sqrt_abs(decimal_places), self.1)
}

/// See `GenericFraction::sqrt_with_accuracy_raw`.
pub fn sqrt_with_accuracy_raw(&self, accuracy: impl Borrow<Accuracy>) -> RawApprox {
self.0.sqrt_with_accuracy_raw(accuracy)
}

/// See `GenericFraction::sqrt_with_accuracy`.
pub fn sqrt_with_accuracy(
&self,
accuracy: impl Borrow<Accuracy>,
) -> GenericDecimal<BigUint, P> {
GenericDecimal(self.0.sqrt_with_accuracy(accuracy), self.1)
}

/// See `GenericFraction::sqrt_raw`.
pub fn sqrt_raw(&self, decimal_places: u32) -> RawApprox {
self.0.sqrt_raw(decimal_places)
}

/// See `GenericFraction::sqrt`.
pub fn sqrt(&self, decimal_places: u32) -> GenericDecimal<BigUint, P> {
GenericDecimal(self.0.sqrt(decimal_places), self.1)
}
}
5 changes: 4 additions & 1 deletion src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ mod postgres_support;
#[cfg(feature = "with-juniper-support")]
mod juniper_support;

#[cfg(feature = "with-approx")]
mod approx;

/// Decimal type implementation
///
/// T is the type for data
Expand Down Expand Up @@ -60,7 +63,7 @@ mod juniper_support;
/// ```
#[derive(Clone)]
#[cfg_attr(feature = "with-serde-support", derive(Serialize, Deserialize))]
pub struct GenericDecimal<T, P>(GenericFraction<T>, P)
pub struct GenericDecimal<T, P>(pub(crate) GenericFraction<T>, pub(crate) P)
where
T: Clone + Integer,
P: Copy + Integer + Into<usize>;
Expand Down

0 comments on commit 35c7871

Please sign in to comment.