Skip to content
Merged
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
159 changes: 75 additions & 84 deletions .gas-snapshot

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/lib/implementation/LibDecimalFloatImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ library LibDecimalFloatImplementation {
returns (int256, int256)
{
unchecked {
(signedCoefficientA, exponentA) = normalize(signedCoefficientA, exponentA);
(signedCoefficientA, exponentA) = maximize(signedCoefficientA, exponentA);
(signedCoefficientB, exponentB) = normalize(signedCoefficientB, exponentB);

int256 signedCoefficient = (signedCoefficientA * 1e38) / signedCoefficientB;
int256 exponent = exponentA - exponentB - 38;
int256 signedCoefficient = signedCoefficientA / signedCoefficientB;
int256 exponent = exponentA - exponentB;
return (signedCoefficient, exponent);
}
}
Expand Down Expand Up @@ -371,8 +371,8 @@ library LibDecimalFloatImplementation {
function inv(int256 signedCoefficient, int256 exponent) internal pure returns (int256, int256) {
(signedCoefficient, exponent) = normalize(signedCoefficient, exponent);

signedCoefficient = 1e75 / signedCoefficient;
exponent = -exponent - 75;
signedCoefficient = 1e76 / signedCoefficient;
exponent = -exponent - 76;

return (signedCoefficient, exponent);
}
Expand Down
4 changes: 2 additions & 2 deletions test/lib/LibCommonResults.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.25;

int256 constant ONES = 11111111111111111111111111111111111111;
int256 constant THREES = 33333333333333333333333333333333333333;
int256 constant ONES = 111111111111111111111111111111111111111;
int256 constant THREES = 333333333333333333333333333333333333333;
Comment thread
thedavidmeister marked this conversation as resolved.
6 changes: 3 additions & 3 deletions test/src/lib/LibDecimalFloat.mixed.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ contract LibDecimalFloatMixedTest is Test {
Float c = a.div(b);
(int256 signedCoefficientDiv, int256 exponentDiv) = LibDecimalFloat.unpack(c);
assertEq(signedCoefficientDiv, THREES, "coefficient");
assertEq(exponentDiv, -38, "exponent");
assertEq(exponentDiv, -39, "exponent");

Float d = c.mul(LibDecimalFloat.packLossless(555, 18));
(int256 signedCoefficientMul, int256 exponentMul) = LibDecimalFloat.unpack(d);

assertEq(signedCoefficientMul, 18499999999999999999999999999999999999815);
assertEq(exponentMul, -20);
assertEq(signedCoefficientMul, 184999999999999999999999999999999999999815);
assertEq(exponentMul, -21);
}
}
4 changes: 2 additions & 2 deletions test/src/lib/LibDecimalFloat.pow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ contract LibDecimalFloatPowTest is LogTest {
}

function testPows() external {
checkPow(5e37, -38, 3e37, -36, 9.3283582089552238805970149253731343283e37, -47);
checkPow(5e37, -38, 6e37, -36, 8.7108013937282229965156794425087108013e37, -56);
checkPow(5e37, -38, 3e37, -36, 9.32835820895522388059701492537313432835e38, -48);
checkPow(5e37, -38, 6e37, -36, 8.71080139372822299651567944250871080139e38, -57);
// // Issues found in fuzzing from here.
checkPow(99999, 0, 12182, 0, 1000, 60907);
checkPow(1785215562, 0, 18, 0, 3388, 163);
Expand Down
22 changes: 11 additions & 11 deletions test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ contract LibDecimalFloatImplementationDivTest is Test {

/// 1 / 3
function testDiv1Over3() external pure {
checkDiv(1, 0, 3, 0, THREES, -38);
checkDiv(1, 0, 3, 0, THREES, -39);
}
Comment thread
thedavidmeister marked this conversation as resolved.

/// - 1 / 3
function testDivNegative1Over3() external pure {
checkDiv(-1, 0, 3, 0, -THREES, -38);
checkDiv(-1, 0, 3, 0, -THREES, -39);
}
Comment thread
thedavidmeister marked this conversation as resolved.

/// 1 / 3 gas
Expand All @@ -56,41 +56,41 @@ contract LibDecimalFloatImplementationDivTest is Test {

/// 1e18 / 3
function testDiv1e18Over3() external pure {
checkDiv(1e18, 0, 3, 0, THREES, -20);
checkDiv(1e18, 0, 3, 0, THREES, -21);
}
Comment thread
thedavidmeister marked this conversation as resolved.

/// 10,0 / 1e38,-37 == 1
function testDivTenOverOOMs() external pure {
checkDiv(10, 0, 1e38, -37, 1e38, -38);
checkDiv(10, 0, 1e38, -37, 1e39, -39);
}
Comment thread
thedavidmeister marked this conversation as resolved.

/// 1e38,-37 / 2,0 == 5
function testDivOOMsOverTen() external pure {
checkDiv(1e38, -37, 2, 0, 5e37, -37);
checkDiv(1e38, -37, 2, 0, 5e38, -38);
}
Comment on lines +69 to 70
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Express -38 as 1 - SCALE to tie it to global precision.

This better communicates the relation to the base scale.

-        checkDiv(1e38, -37, 2, 0, 5e38, -38);
+        checkDiv(1e38, -37, 2, 0, 5e38, 1 - SCALE);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
checkDiv(1e38, -37, 2, 0, 5e38, -38);
}
checkDiv(1e38, -37, 2, 0, 5e38, 1 - SCALE);
}
🤖 Prompt for AI Agents
In test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol around
lines 69-70, the exponent literal "-38" should be expressed relative to the
global SCALE constant; replace the hardcoded -38 with "1 - SCALE" so the test
ties the exponent to the library's precision (i.e., change the call
checkDiv(1e38, -37, 2, 0, 5e38, -38) to use 1 - SCALE instead of -38).


/// 5e37,-37 / 2e37,-37 == 2.5
function testDivOOMs5and2() external pure {
checkDiv(5e37, -37, 2e37, -37, 25e37, -38);
checkDiv(5e37, -37, 2e37, -37, 25e38, -39);
}
Comment thread
thedavidmeister marked this conversation as resolved.

/// (1 / 9) / (1 / 3) == 0.333..
function testDiv1Over9Over1Over3() external pure {
// 1 / 9
(int256 signedCoefficientA, int256 exponentA) = LibDecimalFloatImplementation.div(1, 0, 9, 0);
assertEq(signedCoefficientA, ONES);
assertEq(exponentA, -38);
assertEq(exponentA, -39);

Comment thread
thedavidmeister marked this conversation as resolved.
// 1 / 3
(int256 signedCoefficientB, int256 exponentB) = LibDecimalFloatImplementation.div(1, 0, 3, 0);
assertEq(signedCoefficientB, THREES);
assertEq(exponentB, -38);
assertEq(exponentB, -39);

Comment thread
thedavidmeister marked this conversation as resolved.
// (1 / 9) / (1 / 3)
(int256 signedCoefficient, int256 exponent) =
LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB);
assertEq(signedCoefficient, THREES);
assertEq(exponent, -38);
assertEq(signedCoefficient, 333333333333333333333333333333333333336);
assertEq(exponent, -39);
Comment thread
thedavidmeister marked this conversation as resolved.
}

/// forge-config: default.fuzz.runs = 100
Expand All @@ -102,7 +102,7 @@ contract LibDecimalFloatImplementationDivTest is Test {
int256 di = 0;
while (true) {
int256 i = 1;
int256 j = -38 - di;
int256 j = -39 - di;
while (true) {
Comment thread
thedavidmeister marked this conversation as resolved.
// want to see full precision on the THREES regardless of the
// scale of the numerator and denominator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract LibDecimalFloatImplementationPow10Test is LogTest {
checkPow10(0.5e37, -37, 3162, -3);

checkPow10(0.3e37, -37, 1995, -3);
checkPow10(-0.3e37, -37, 5.012531328320802005012531328320802005e37, -38);
checkPow10(-0.3e37, -37, 0.501253132832080200501253132832080200501e39, -39);
}

function testInterpolatedLookupsPower() external {
Expand Down
Loading