solidity-math-utils 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,7 +54,7 @@ Function `floorCbrt(n)` computes the largest integer smaller than or equal to th
54
54
 
55
55
  Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the cubic root of `n`.
56
56
 
57
- Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
57
+ Function `roundDiv(n, d)` computes the nearest integer (half being rounded upwards) to `n / d`.
58
58
 
59
59
  Function `minFactor(x, y)` computes the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`.
60
60
 
@@ -62,7 +62,7 @@ Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal t
62
62
 
63
63
  Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
64
64
 
65
- Function `mulDivR(x, y, z)` computes the nearest integer smaller than or larger than `x * y / z`.
65
+ Function `mulDivR(x, y, z)` computes the nearest integer (half being rounded upwards) to `x * y / z`.
66
66
 
67
67
  Function `mulDivExF(x, y, z, w)` computes the largest integer smaller than or equal to `(x * y) / (z * w)`.
68
68
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-math-utils",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Solidity Mathematical Utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -388,8 +388,8 @@ library AdvancedMath {
388
388
  function lambertPos3(uint256 x) internal pure returns (uint256) { unchecked {
389
389
  uint256 a = AnalyticMath.fixedLog(x);
390
390
  uint256 b = AnalyticMath.fixedLog(a);
391
- uint256 c = IntegralMath.mulDivF(b, b + (a - FIXED_1) * 2, a);
392
- uint256 d = IntegralMath.mulDivF(FIXED_1 / 2, c, a);
391
+ uint256 c = IntegralMath.mulDivF(a, a, FIXED_1 / 2);
392
+ uint256 d = IntegralMath.mulDivF(b, b + (a - FIXED_1) * 2, c);
393
393
  uint256 e = IntegralMath.mulDivF(FIXED_1, a - b + d, x);
394
394
  return e;
395
395
  }}
@@ -34,16 +34,16 @@ library IntegralMath {
34
34
  * @dev Compute the largest integer smaller than or equal to the square root of `n`
35
35
  */
36
36
  function floorSqrt(uint256 n) internal pure returns (uint256) { unchecked {
37
- if (n > 0) {
38
- uint256 x = n / 2 + 1;
39
- uint256 y = (x + n / x) / 2;
37
+ if (n > 63) {
38
+ uint256 x = n;
39
+ uint256 y = 1 << (floorLog2(n) / 2 + 1);
40
40
  while (x > y) {
41
41
  x = y;
42
- y = (x + n / x) / 2;
42
+ y = (x + n / x) >> 1;
43
43
  }
44
44
  return x;
45
45
  }
46
- return 0;
46
+ return 0x7777777777777776666666666666555555555554444444443333333222221110 >> (n * 4) & 0xf;
47
47
  }}
48
48
 
49
49
  /**
@@ -58,16 +58,16 @@ library IntegralMath {
58
58
  * @dev Compute the largest integer smaller than or equal to the cubic root of `n`
59
59
  */
60
60
  function floorCbrt(uint256 n) internal pure returns (uint256) { unchecked {
61
- uint256 x = 0;
62
- for (uint256 y = 1 << 255; y > 0; y >>= 3) {
63
- x <<= 1;
64
- uint256 z = 3 * x * (x + 1) + 1;
65
- if (n / y >= z) {
66
- n -= y * z;
67
- x += 1;
61
+ if (n > 84) {
62
+ uint256 x = n;
63
+ uint256 y = 1 << (floorLog2(n) / 3 + 1);
64
+ while (x > y) {
65
+ x = y;
66
+ y = ((x << 1) + n / x ** 2) / 3;
68
67
  }
68
+ return x;
69
69
  }
70
- return x;
70
+ return 0x49249249249249246db6db6db6db6db6db6db6db6db692492492492492249248 >> (n * 3) & 0x7;
71
71
  }}
72
72
 
73
73
  /**
@@ -79,7 +79,7 @@ library IntegralMath {
79
79
  }}
80
80
 
81
81
  /**
82
- * @dev Compute the nearest integer to the quotient of `n` and `d` (or `n / d`)
82
+ * @dev Compute the nearest integer (half being rounded upwards) to `n / d`
83
83
  */
84
84
  function roundDiv(uint256 n, uint256 d) internal pure returns (uint256) { unchecked {
85
85
  return n / d + (n % d) / (d - d / 2);
@@ -177,7 +177,7 @@ library IntegralMath {
177
177
  }}
178
178
 
179
179
  /**
180
- * @dev Compute the nearest integer smaller than or larger than `x * y / z`
180
+ * @dev Compute the nearest integer (half being rounded upwards) to `x * y / z`
181
181
  */
182
182
  function mulDivR(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
183
183
  uint256 w = mulDivF(x, y, z);