Skip to content
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

sqrt implementation #84

Merged
merged 31 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
1c4158c
add draft `sqrt` implementation
squ1dd13 Aug 25, 2023
a16fb20
Merge remote-tracking branch 'upstream/master'
squ1dd13 Sep 3, 2023
b7ee380
hide `ToBigInt` behind `with-bigint`
squ1dd13 Sep 3, 2023
780b464
move arg into format string
squ1dd13 Sep 3, 2023
55c28e9
document panics
squ1dd13 Sep 3, 2023
d9e722f
use `BigUint` for `approx` and hide module behind `with-bigint`
squ1dd13 Sep 4, 2023
881f4b8
require `with-dynaint` for `approx` for now
squ1dd13 Sep 4, 2023
b75ab36
don't force `sqrt` return values into `DynaFraction`s
squ1dd13 Sep 4, 2023
1147631
remove `with-dynaint` for `approx`
squ1dd13 Sep 4, 2023
ea7c007
minor tidying
squ1dd13 Sep 4, 2023
34e196d
improve `SqrtApprox` API
squ1dd13 Sep 4, 2023
e66b580
replace `SqrtAccuracy` with new `Accuracy` API
squ1dd13 Sep 5, 2023
1f2e17c
minor optimisations in `sqrt`
squ1dd13 Sep 5, 2023
2e44b9a
minor cleanup in `Accuracy`
squ1dd13 Sep 5, 2023
27e846a
move `sqrt_setup` inside `SqrtSetup` (as `for_value`)
squ1dd13 Sep 5, 2023
3149e84
write real tests for square roots
squ1dd13 Sep 5, 2023
c3fef68
remove unused import
squ1dd13 Sep 5, 2023
0c47d63
use `lazy_static` and `const` instead of functions in sqrt tests
squ1dd13 Sep 5, 2023
f15c393
add benchmarks for `sqrt`
squ1dd13 Sep 5, 2023
b1fd030
draft `sqrt_abs` API
squ1dd13 Sep 19, 2023
65db348
get rid of unnecessary `unwrap`
squ1dd13 Sep 19, 2023
36c2b60
add `sqrt` module; write documentation for all `sqrt` stuff
squ1dd13 Sep 19, 2023
4baf1fb
add missing `sqrt` tests
squ1dd13 Sep 19, 2023
4e0b410
gate `approx` behind `with-approx`
squ1dd13 Sep 19, 2023
260fffc
remove `lazy_static` as dev dependency
squ1dd13 Sep 19, 2023
345a32e
document `with-approx` in `README`
squ1dd13 Sep 19, 2023
35c7871
add `sqrt` methods for `GenericDecimal`
squ1dd13 Sep 19, 2023
5fd5765
do not panic in `sqrt_*` when given a negative value
squ1dd13 Sep 24, 2023
6c66572
optimise tests by default
squ1dd13 Sep 25, 2023
828c671
add note about `sqrt` performance in debug builds
squ1dd13 Sep 25, 2023
d2dbc9f
hide `sqrt` benches behind `with-approx`
squ1dd13 Sep 26, 2023
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ with-serde-support = ["serde", "serde_derive", "num/serde"]
[dev-dependencies]
criterion = "0.4"
rand = "0.8.5"
lazy_static = "1"
dnsl48 marked this conversation as resolved.
Show resolved Hide resolved

[[bench]]
name = "bench_fraction"
Expand Down
43 changes: 35 additions & 8 deletions benches/bench_fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use fraction::generic;
use fraction::{GenericDecimal, GenericFraction};
use std::str::FromStr;

#[allow(clippy::missing_panics_doc)]
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)))
b.iter(|| GenericDecimal::<u128, u16>::from(black_box(15978.649)));
});

c.bench_function("Decimal i64/u16 init", |b| {
Expand All @@ -16,7 +17,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let b = GenericDecimal::<i64, u16>::from(black_box(-15978.649));

(a, b)
})
});
});

c.bench_function("Convert int like from str", |b| {
Expand All @@ -25,7 +26,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let b = GenericFraction::<u8>::from_str(black_box("100"));

(a, b)
})
});
});

c.bench_function("Convert float like from str", |b| {
Expand All @@ -34,28 +35,54 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let b = GenericFraction::<u8>::from_str(black_box("100.001"));

(a, b)
})
});
});

c.bench_function("Convert fraction like from str", |b| {
b.iter(|| {
let a = GenericFraction::<u8>::from_str(black_box("1/1"));
let b = GenericFraction::<u8>::from_str(black_box("255/255"));
(a, b)
})
});
});

c.bench_function("generic::read_generic_integer / i8 to u8", |b| {
b.iter(|| generic::read_generic_integer::<u8, i8>(black_box(14i8)).unwrap())
b.iter(|| generic::read_generic_integer::<u8, i8>(black_box(14i8)).unwrap());
});

c.bench_function("generic::read_generic_integer / u8 to u8", |b| {
b.iter(|| generic::read_generic_integer::<u8, u8>(black_box(14u8)).unwrap())
b.iter(|| generic::read_generic_integer::<u8, u8>(black_box(14u8)).unwrap());
});

c.bench_function("From couple", |b| {
b.iter(|| GenericFraction::<u8>::from(black_box((3u8, 4u8))))
b.iter(|| GenericFraction::<u8>::from(black_box((3u8, 4u8))));
});

let num2 = GenericFraction::<u8>::from(2);
let small_num = fraction::BigFraction::new(1_u8, u128::MAX) / u128::MAX;
let big_num = fraction::BigFraction::new(u128::MAX, 1_u8) * u128::MAX;

let mut bench_dp = |n: u32| {
let mut group = c.benchmark_group(format!("Sqrt {n}dp raw"));

group.bench_function("2", |b| {
b.iter(|| num2.sqrt_raw(n));
});

group.bench_function("Small", |b| {
b.iter(|| small_num.sqrt_raw(n));
});

group.bench_function("Big", |b| {
b.iter(|| big_num.sqrt_raw(n));
});

group.finish();
};

bench_dp(10_000);
bench_dp(1_000);
bench_dp(100);
}

criterion_group!(benches, criterion_benchmark);
Expand Down
29 changes: 29 additions & 0 deletions src/dynaint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use std::mem;

use num::{
bigint::{ToBigInt, ToBigUint},
Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Integer, Num, One, ToPrimitive, Zero,
};
use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
Expand Down Expand Up @@ -886,6 +887,34 @@ where
dyna_impl!(impl_fn_refmath_tuple2self; div_rem);
}

#[cfg(feature = "with-bigint")]
impl<T, G> ToBigInt for DynaInt<T, G>
squ1dd13 marked this conversation as resolved.
Show resolved Hide resolved
where
T: Copy + GenericInteger + Into<G> + TryToConvertFrom<G> + From<u8> + ToBigInt,
G: Clone + GenericInteger + ToBigInt,
{
fn to_bigint(&self) -> Option<num::BigInt> {
match self {
DynaInt::S(s) => s.to_bigint(),
DynaInt::__H(h) => h.to_bigint(),
}
}
}

#[cfg(feature = "with-bigint")]
impl<T, G> ToBigUint for DynaInt<T, G>
where
T: Copy + GenericInteger + Into<G> + TryToConvertFrom<G> + From<u8> + ToBigUint,
G: Clone + GenericInteger + ToBigUint,
{
fn to_biguint(&self) -> Option<num::BigUint> {
match self {
DynaInt::S(s) => s.to_biguint(),
DynaInt::__H(h) => h.to_biguint(),
}
}
}

#[cfg(test)]
mod tests {
use super::{
Expand Down
Loading
Loading