Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
34 lines (28 sloc) 882 Bytes
pragma solidity 0.4.25;
import "./SafeDecimalMath.sol";
library Math {
using SafeMath for uint;
using SafeDecimalMath for uint;
/**
* @dev Uses "exponentiation by squaring" algorithm where cost is 0(logN)
* vs 0(N) for naive repeated multiplication.
* Calculates x^n with x as fixed-point and n as regular unsigned int.
* Calculates to 18 digits of precision with SafeDecimalMath.unit()
*/
function powDecimal(uint x, uint n)
internal
pure
returns (uint)
{
// https://mpark.github.io/programming/2014/08/18/exponentiation-by-squaring/
uint result = SafeDecimalMath.unit();
while (n > 0) {
if (n % 2 != 0) {
result = result.multiplyDecimal(x);
}
x = x.multiplyDecimal(x);
n /= 2;
}
return result;
}
}
You can’t perform that action at this time.