-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(test/Circles): test issuance, consecutive periods
1 parent
23ecf4d
commit 53b2741
Showing
2 changed files
with
61 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
pragma solidity >=0.8.13; | ||
|
||
import "../../src/lib/Math64x64.sol"; | ||
|
||
contract Approximation { | ||
function approximatelyEqual(uint256 a, uint256 b, uint256 epsilon) public pure returns (bool) { | ||
return a > b ? a - b <= epsilon : b - a <= epsilon; | ||
// Constants | ||
|
||
int128 private constant ONE = int128(2 ** 64); | ||
|
||
// 1% in 64x64 fixed point: integer approximation of 2**64 / 100 | ||
int128 internal constant ONE_PERCENT = int128(184467440737095516); | ||
|
||
function approximatelyEqual(uint256 _a, uint256 _b, uint256 _epsilon) public pure returns (bool) { | ||
return _a > _b ? _a - _b <= _epsilon : _b - _a <= _epsilon; | ||
} | ||
|
||
function relativeApproximatelyEqual(uint256 _a, uint256 _b, int128 _epsilon) public pure returns (bool) { | ||
require(_epsilon >= 0, "Approximation: negative epsilon"); | ||
require(_epsilon <= ONE, "Approximation: epsilon too large"); | ||
if (_a == _b) { | ||
return true; | ||
} | ||
if (_a == 0 || _b == 0) { | ||
return _epsilon == ONE; | ||
} | ||
|
||
// calculate the absolute difference | ||
uint256 diff = _a > _b ? _a - _b : _b - _a; | ||
|
||
// use the larger of the two values as denominator | ||
uint256 max = _a > _b ? _a : _b; | ||
|
||
// calculate the relative difference | ||
int128 relDiff = Math64x64.divu(diff, max); | ||
|
||
return relDiff <= _epsilon; | ||
} | ||
} |