Skip to content

Commit

Permalink
change exp function logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DrunkRandomWalker committed Sep 21, 2023
1 parent a1ef64d commit 0d411e4
Showing 1 changed file with 42 additions and 34 deletions.
76 changes: 42 additions & 34 deletions packages/injective-math/src/fp_decimal/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ impl FPDecimal {
Ordering::Less => {
// NOTE: only accurate for 1,3,5,7,11, and combinations of these numbers
if a.log2().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
a = a.sqrt();
tmp_b /= FPDecimal::TWO;
}
return Ok(FPDecimal::ONE / a);
};

if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -383,6 +392,9 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log3().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -391,9 +403,7 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log3().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -404,6 +414,9 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log5().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -412,10 +425,7 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log5().is_some() {
println!("here");
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -426,6 +436,9 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log7().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -434,9 +447,7 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log7().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -447,6 +458,9 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log11().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -455,9 +469,7 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
}

if a.log11().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -468,14 +480,6 @@ impl FPDecimal {
}
return Ok(FPDecimal::ONE / a);
};
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
a = a.sqrt();
tmp_b /= FPDecimal::TWO;
}
return Ok(FPDecimal::ONE / a);
};
}

Ok(FPDecimal::_exp_taylor_expansion(FPDecimal::ONE / a, exponent, n_terms))
Expand Down Expand Up @@ -508,6 +512,15 @@ impl FPDecimal {
// taylor expansion approximation of exponentation compuation with float number exponent
// NOTE: only accurate for 1,3,5,7,11, and combinations of these numbers
if a.log2().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
a = a.sqrt();
tmp_b /= FPDecimal::TWO;
}
return Ok(a);
};

if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -518,6 +531,9 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log3().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -526,9 +542,7 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log3().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -539,6 +553,9 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log5().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -547,10 +564,7 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log5().is_some() {
println!("here");
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -561,6 +575,9 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log7().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -569,9 +586,7 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log7().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -582,6 +597,9 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log11().is_some() {
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
Expand All @@ -590,9 +608,7 @@ impl FPDecimal {
}
return Ok(a);
};
}

if a.log11().is_some() {
if ((FPDecimal::reciprocal(exponent) % FPDecimal::TWO).int() - FPDecimal::ONE).abs()
<= FPDecimal::must_from_str("0.000001")
{
Expand All @@ -603,14 +619,6 @@ impl FPDecimal {
}
return Ok(a);
};
if FPDecimal::reciprocal(exponent) % FPDecimal::TWO == FPDecimal::ZERO {
let mut tmp_b = FPDecimal::reciprocal(exponent).int();
while tmp_b > FPDecimal::ONE {
a = a.sqrt();
tmp_b /= FPDecimal::TWO;
}
return Ok(a);
};
}

Ok(FPDecimal::_exp_taylor_expansion(a, exponent, n_terms))
Expand Down

0 comments on commit 0d411e4

Please sign in to comment.