Skip to content

Commit

Permalink
feat: finish up from_scaled implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Sep 15, 2024
1 parent 87dbf77 commit c8544d5
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to `const-decimal`.

- Added `BorshSerialize` and `BorshDeserialize` behind `borsh` feature flag.
- Added `AddAssign`, `SubAssign`, `MulAssign`, and `DivAssign`.
- Implemented `Decimal::from_scaled(integer: I, decimals: u8)`.

## 0.1.0

Expand Down
6 changes: 3 additions & 3 deletions benches/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use const_decimal::{Decimal, Integer};
use const_decimal::{Decimal, ScaledInteger};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
Expand All @@ -8,7 +8,7 @@ use proptest::prelude::*;

pub fn bench_all<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Integer<D> + Arbitrary,
I: ScaledInteger<D> + Arbitrary,
{
bench_primitive_add::<I>(group);
bench_decimal_add::<D, I>(group);
Expand All @@ -33,7 +33,7 @@ where

fn bench_decimal_add<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Integer<D> + Arbitrary,
I: ScaledInteger<D> + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
6 changes: 3 additions & 3 deletions benches/div.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::ops::Div;

use const_decimal::{Decimal, Integer};
use const_decimal::{Decimal, ScaledInteger};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::{ConstOne, ConstZero, PrimInt};
Expand All @@ -14,7 +14,7 @@ pub fn bench_all<const D: u8, I>(
lo_strategy: impl Strategy<Value = I> + Clone,
hi_strategy: impl Strategy<Value = I> + Clone,
) where
I: Integer<D> + Debug + Div<Output = I>,
I: ScaledInteger<D> + Debug + Div<Output = I>,
{
primitive_div::<I>(group, lo_strategy.clone(), "lo");
decimal_div::<D, I>(group, lo_strategy, "lo");
Expand Down Expand Up @@ -56,7 +56,7 @@ fn decimal_div<const D: u8, I>(
strategy: impl Strategy<Value = I> + Clone,
strategy_label: &str,
) where
I: Integer<D> + Debug,
I: ScaledInteger<D> + Debug,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
4 changes: 2 additions & 2 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::ops::{Div, Neg};

use const_decimal::Integer;
use const_decimal::ScaledInteger;
use criterion::measurement::WallTime;
use criterion::BenchmarkGroup;
use num_traits::ConstOne;
Expand Down Expand Up @@ -67,7 +67,7 @@ fn bench_integers<const D: u8, I>(
hi_mul_range: impl Strategy<Value = I> + Clone + Debug,
hi_div_range: impl Strategy<Value = I> + Clone + Debug,
) where
I: Integer<D> + Arbitrary + Div<Output = I>,
I: ScaledInteger<D> + Arbitrary + Div<Output = I>,
{
add::bench_all::<D, I>(group);
sub::bench_all::<D, I>(group);
Expand Down
6 changes: 3 additions & 3 deletions benches/mul.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;

use const_decimal::{Decimal, Integer};
use const_decimal::{Decimal, ScaledInteger};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
Expand All @@ -13,7 +13,7 @@ pub fn bench_all<const D: u8, I>(
lo_strategy: impl Strategy<Value = I> + Clone,
hi_strategy: impl Strategy<Value = I> + Clone,
) where
I: Integer<D> + Debug,
I: ScaledInteger<D> + Debug,
{
primitive_mul::<I>(group, lo_strategy.clone(), "lo");
decimal_mul::<D, I>(group, lo_strategy, "lo");
Expand Down Expand Up @@ -46,7 +46,7 @@ fn decimal_mul<const D: u8, I>(
strategy: impl Strategy<Value = I> + Clone,
strategy_label: &str,
) where
I: Integer<D> + Debug,
I: ScaledInteger<D> + Debug,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
6 changes: 3 additions & 3 deletions benches/sub.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use const_decimal::{Decimal, Integer};
use const_decimal::{Decimal, ScaledInteger};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
Expand All @@ -8,7 +8,7 @@ use proptest::prelude::*;

pub fn bench_all<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Integer<D> + Arbitrary,
I: ScaledInteger<D> + Arbitrary,
{
bench_primitive_sub::<I>(group);
bench_decimal_sub::<D, I>(group);
Expand All @@ -33,7 +33,7 @@ where

fn bench_decimal_sub<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: Integer<D> + Arbitrary,
I: ScaledInteger<D> + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
Expand Down
11 changes: 11 additions & 0 deletions src/algorithms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::ScaledInteger;

pub(crate) fn log10<const D: u8, I: ScaledInteger<D>>(mut input: I) -> I {
let mut result = I::ZERO;
while input > I::ZERO {
input /= I::TEN;
result += I::ONE;
}

result
}
Loading

0 comments on commit c8544d5

Please sign in to comment.