Skip to content

Use a GCD initialization for f32 and f64 #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fraction"
version = "0.7.0"
version = "0.8.0"
authors = ["dnsl48 <[email protected]>"]

description = "Lossless fractions and decimals; drop-in float replacement"
Expand Down Expand Up @@ -47,3 +47,10 @@ with-dynaint = []
with-juniper-support = ["juniper"]
with-postgres-support = ["postgres", "byteorder"]
with-serde-support = ["serde", "serde_derive", "num/serde"]

[dev-dependencies]
criterion = "0.3"

[[bench]]
name = "bench_fraction"
harness = false
22 changes: 22 additions & 0 deletions benches/bench_fraction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate criterion;
extern crate fraction;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use fraction::GenericDecimal;

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("Decimal u128/u16 init", |b| {
b.iter(|| {
GenericDecimal::<u128, u16>::from(black_box(15978.649));
})
});

c.bench_function("Decimal i64/u16 init", |b| {
b.iter(|| {
GenericDecimal::<i64, u16>::from(black_box(15978.649));
GenericDecimal::<i64, u16>::from(black_box(-15978.649));
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
15 changes: 6 additions & 9 deletions src/decimal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use error;

use num::integer::Integer;
use num::traits::{
/*Float, */ Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Num, One, Signed,
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Signed,
ToPrimitive, Zero,
};

Expand Down Expand Up @@ -97,7 +97,7 @@ where
match *self {
GenericDecimal(ref fraction, precision) => {
let prec = precision.into();
let debug_prec = f.precision().unwrap_or_else(|| 32);
let debug_prec = f.precision().unwrap_or(32);
write!(
f,
"GenericDecimal({} | prec={}; {:?}; {})",
Expand Down Expand Up @@ -285,17 +285,14 @@ macro_rules! dec_impl {
(impl_trait_from_float; $($t:ty),*) => {$(
impl<T, P> From<$t> for GenericDecimal<T, P>
where
T: Clone + GenericInteger,
P: Copy + GenericInteger + Into<usize> + From<u8>
T: Clone + GenericInteger + FromPrimitive,
P: Copy + GenericInteger + Into<usize> + From<u8> + Bounded
{
fn from(value: $t) -> Self {
if value.is_nan () { return GenericDecimal::nan() };
if value.is_infinite () { return if value.is_sign_negative () { GenericDecimal::neg_infinity() } else { GenericDecimal::infinity() } };

/* TODO: without the String conversion (probably through .to_bits) */
let src = format! ("{:+}", value);

GenericDecimal::from_decimal_str(&src).unwrap_or_else(|_| GenericDecimal::nan())
GenericDecimal(GenericFraction::from(value), P::zero()).calc_precision(None)
}
}
)*}
Expand Down Expand Up @@ -848,7 +845,7 @@ where
GenericFraction::Infinity(_) => P::zero(),
GenericFraction::Rational(_, ref ratio) => {
let mut precision: P = P::zero();
let max_precision: P = max_precision.unwrap_or_else(|| P::max_value());
let max_precision: P = max_precision.unwrap_or(P::max_value());

let num = ratio.numer();
let den = ratio.denom();
Expand Down
5 changes: 1 addition & 4 deletions src/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ pub struct DivisionState<I> {

impl<I> DivisionState<I> {
pub fn new(remainder: I, divisor: I) -> Self {
DivisionState {
remainder: remainder,
divisor: divisor,
}
DivisionState { remainder, divisor }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/dynaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,8 @@ where

fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
T::from_str_radix(s, radix)
.map(|v| DynaInt::S(v))
.or_else(|_| G::from_str_radix(s, radix).map(|v| DynaInt::h(v)))
.map(DynaInt::S)
.or_else(|_| G::from_str_radix(s, radix).map(DynaInt::h))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fraction/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Format {
align: formatter.align(),
width: formatter.width(),
precision: formatter.precision(),
flags: flags,
flags,
}
}

Expand Down
Loading