Skip to content

Commit

Permalink
BigFloat - ln optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Jun 12, 2024
1 parent 20acd9e commit b86ac8e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions framework/base/src/types/managed/basic/big_float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,20 @@ impl<M: ManagedTypeApi> BigFloat<M> {
debug_assert!(x >= 1);
debug_assert!(x <= 2);

let mut result = BigFloat::from_frac(-56570851i64, 1_000_000_000i64); // -0.056570851, 9 decimals;
result *= &x;
result += BigFloat::from_frac(44717955i64, 100_000_000i64); // 0.44717955, 8 decimals;
result *= &x;
result += BigFloat::from_frac(-14699568i64, 10_000_000i64); // -1.4699568, 7 decimals;
result *= &x;
result += BigFloat::from_frac(28212026i64, 10_000_000i64); // 2.8212026, 7 decimals;
result *= &x;
result += BigFloat::from_frac(-17417939i64, 10_000_000i64); // -1.7417939, 7 decimals;

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(bit_log2)) * ln_of_2;
result + add_member
result += BigFloat::from(bit_log2 as i32) * ln_of_2;

result
}
}

Expand Down

0 comments on commit b86ac8e

Please sign in to comment.