Skip to content

Commit

Permalink
initial impl and test
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Jun 12, 2024
1 parent a2e4697 commit f2a3463
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ fn big_float_overflow_test_rs() {
assert_eq!(forth, &BigUint::from(2184473079534488064u64));
}

#[test]
fn big_float_ln_test_rs() {
let fixed = BigFloat::<StaticApi>::from_frac(23i64, 2i64);

let ln_fixed = fixed.ln(BigUint::from(10u64)); // precision of 10 decimal points

println!("{ln_fixed:?}");
assert_eq!(
ln_fixed,
BigFloat::from_frac(31355146488i64, 10_000_000_000i64)
);
}

#[test]
fn big_float_new_from_big_int_rs() {
world().run("scenarios/big_float_new_from_big_int.scen.json");
Expand Down
29 changes: 27 additions & 2 deletions framework/base/src/types/managed/basic/big_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,33 @@ impl<M: ManagedTypeApi> BigFloat<M> {
(self * denominator).trunc()
}

pub fn to_managed_decimal<T: Decimals>(self, decimals: T) -> ManagedDecimal<M, T> {
ManagedDecimal::<M, T>::from_big_float(self, decimals)
pub fn to_managed_decimal<T: Decimals>(&self, decimals: T) -> ManagedDecimal<M, T> {
ManagedDecimal::<M, T>::from_big_float(&self, decimals)

Check warning on line 172 in framework/base/src/types/managed/basic/big_float.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/base/src/types/managed/basic/big_float.rs#L172

warning: this expression creates a reference which is immediately dereferenced by the compiler --> framework/base/src/types/managed/basic/big_float.rs:172:48 | 172 | ManagedDecimal::<M, T>::from_big_float(&self, decimals) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
framework/base/src/types/managed/basic/big_float.rs:172:48:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> framework/base/src/types/managed/basic/big_float.rs:172:48
    |
172 |         ManagedDecimal::<M, T>::from_big_float(&self, decimals)
    |                                                ^^^^^ help: change this to: `self`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__

Check warning on line 172 in framework/base/src/types/managed/basic/big_float.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/base/src/types/managed/basic/big_float.rs#L172

warning: this expression creates a reference which is immediately dereferenced by the compiler --> framework/base/src/types/managed/basic/big_float.rs:172:48 | 172 | ManagedDecimal::<M, T>::from_big_float(&self, decimals) | ^^^^^ help: change this to: `self` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Raw output
framework/base/src/types/managed/basic/big_float.rs:172:48:w:warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> framework/base/src/types/managed/basic/big_float.rs:172:48
    |
172 |         ManagedDecimal::<M, T>::from_big_float(&self, decimals)
    |                                                ^^^^^ help: change this to: `self`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default


__END__
}

pub fn ln(&self, precision: BigUint<M>) -> Self {
// find the highest power of 2 less than or equal to self
let mng_dec = self
.to_fixed_point(&BigFloat::from(precision))
.into_big_uint()
.unwrap_or_sc_panic("can't calculate ln for this number");

let log2 = mng_dec.log2(); // most significant bit
let divisor = BigFloat::from(1 << log2);
let x = self / &divisor; // normalize to [1.0, 2.0]

let ln_of_2 = BigFloat::from_frac(69314718i64, 100_000_000i64); // 0.69314718 8 decimals
let first = BigFloat::from_frac(17417939i64, 10_000_000i64); // 1.7417939, 7 decimals
let second = BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals
let third = BigFloat::from_frac(14699568i64, 10_000_000i64); // 1.4699568, 7 decimals
let fourth = BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals
let fifth = BigFloat::from_frac(56570851i64, 1_000_000_000i64); // 0.056570851, 9 decimals

// approximating polynom to get the result
let result =
(((fourth - fifth * x.clone()) * x.clone() - third) * x.clone() + second) * x - first;
let add_member = BigFloat::from_big_uint(&BigUint::from(log2)) * ln_of_2;
result + add_member
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl<M: ManagedTypeApi, D: Decimals> ManagedDecimal<M, D> {
}

pub fn from_big_float<T: Decimals>(
big_float: BigFloat<M>,
big_float: &BigFloat<M>,
num_decimals: T,
) -> ManagedDecimal<M, T> {
let scaling_factor: &BigUint<M> = &num_decimals.scaling_factor();
Expand Down
4 changes: 2 additions & 2 deletions framework/scenario/tests/managed_decimal_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ pub fn test_managed_decimal() {

let float_1 = BigFloat::<StaticApi>::from_frac(3i64, 2i64);
let fixed_float_1 = ManagedDecimal::<StaticApi, ConstDecimals<1>>::from_big_float(
float_1.clone(),
&float_1,
ConstDecimals::<1>,
);
let fixed_float_2 = ManagedDecimal::<StaticApi, NumDecimals>::from_big_float(float_1, 1usize);
let fixed_float_2 = ManagedDecimal::<StaticApi, NumDecimals>::from_big_float(&float_1, 1usize);

assert_eq!(
fixed_float_1,
Expand Down

0 comments on commit f2a3463

Please sign in to comment.