Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create an EVM package #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 95 additions & 2 deletions contracts/FixidityLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ library FixidityLib {
/**
* @notice Converts an int256 which is already in some fixed point
* representation to that of this library. The _originDigits parameter is the
* precision of x. Values with a precision higher than FixidityLib.digits()
* precision of x. Values with a precision higher than digits()
* will be truncated accordingly.
*/
function newFixed(int256 x, uint8 _originDigits)
Expand All @@ -240,7 +240,7 @@ library FixidityLib {
* @notice Converts an int256 in the fixed point representation of this
* library to a different representation. The _destinationDigits parameter is the
* precision of the output x. Values with a precision below than
* FixidityLib.digits() will be truncated accordingly.
* digits() will be truncated accordingly.
*/
function fromFixed(int256 x, uint8 _destinationDigits)
public
Expand Down Expand Up @@ -440,4 +440,97 @@ library FixidityLib {
assert(y <= maxFixedDivisor());
return multiply(x, reciprocal(y));
}

/**
* @notice This is e in the fixed point units used in this library.
* @dev 27182818284590452353602874713526624977572470936999595749669676277240766303535/fixed1()
* Hardcoded to 24 digits.
*/
function fixedE() public pure returns(int256) {
return 2718281828459045235360287;
}

/**
* @notice ln(1.5), hardcoded with the comma 24 positions to the right.
*/
// solium-disable-next-line mixedcase
function fixedLn1_5() public pure returns(int256) {
return 405465108108164381978013;
}

/**
* @notice ln(10), hardcoded with the comma 24 positions to the right.
*/
function fixedLn10() public pure returns(int256) {
return 2302585092994045684017991;
}

/**
* @notice ln(x)
* This function has a 1/50 deviation close to ln(-1),
* 1/maxFixedMul() deviation at fixedE()**2, but diverges to 10x
* deviation at maxNewFixed().
* @dev
* Test ln(0) fails
* Test ln(-fixed1()) fails
* Test ln(fixed1()) returns 0
* Test ln(fixedE()) returns fixed1()
* Test ln(fixedE()*fixedE()) returns ln(fixedE())+ln(fixedE())
* Test ln(maxInt256) returns 176752531042786059920093411119162458112
* Test ln(1) returns -82
*/
function ln(int256 value) public pure returns (int256) {
assert(value >= 0);
int256 v = value;
int256 r = 0;
while (v <= fixed1() / 10) {
v = v * 10;
r -= fixedLn10();
}
while (v >= 10 * fixed1()) {
v = v / 10;
r += fixedLn10();
}
while (v < fixed1()) {
v = multiply(v, fixedE());
r -= fixed1();
}
while (v > fixedE()) {
v = divide(v, fixedE());
r += fixed1();
}
if (v == fixed1()) {
return r;
}
if (v == fixedE()) {
return fixed1() + r;
}

v = v - 3 * fixed1() / 2;
r = r + fixedLn1_5();
int256 m = fixed1() * v / (v + 3 * fixed1());
r = r + 2 * m;
// solium-disable-next-line mixedcase
int256 m_2 = m * m / fixed1();
uint8 i = 3;
while (true) {
m = m * m_2 / fixed1();
r = r + 2 * m / int256(i);
i += 2;
if (i >= 3 + 2 * digits()) break;
}
return r;
}

/**
* @notice log_b(x).
* *param int256 b Base in fixed point representation.
* @dev Tests covered by ln(x) and divide(a,b)
*/
// solium-disable-next-line mixedcase
function log_b(int256 b, int256 x) public pure returns (int256) {
if (b == fixed1()*10)
return divide(ln(x), fixedLn10());
return divide(ln(x), ln(b));
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4",
"ethlint": "1.2.3"
"ethlint": "1.2.3",
"eth-gas-reporter": "^0.1.0"
}
}
}
3 changes: 1 addition & 2 deletions truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module.exports = {
},

mocha: {
// see more here: https://www.npmjs.com/package/eth-gas-reporter
// reporter: 'eth-gas-reporter',
reporter: 'eth-gas-reporter',
},

solc: {
Expand Down