-
Notifications
You must be signed in to change notification settings - Fork 2
update div precision #104
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
update div precision #104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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; | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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); | ||||||||||
| } | ||||||||||
|
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); | ||||||||||
| } | ||||||||||
|
thedavidmeister marked this conversation as resolved.
|
||||||||||
|
|
||||||||||
| /// 1 / 3 gas | ||||||||||
|
|
@@ -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); | ||||||||||
| } | ||||||||||
|
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); | ||||||||||
| } | ||||||||||
|
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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| /// 5e37,-37 / 2e37,-37 == 2.5 | ||||||||||
| function testDivOOMs5and2() external pure { | ||||||||||
| checkDiv(5e37, -37, 2e37, -37, 25e37, -38); | ||||||||||
| checkDiv(5e37, -37, 2e37, -37, 25e38, -39); | ||||||||||
| } | ||||||||||
|
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); | ||||||||||
|
|
||||||||||
|
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); | ||||||||||
|
|
||||||||||
|
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); | ||||||||||
|
thedavidmeister marked this conversation as resolved.
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| /// forge-config: default.fuzz.runs = 100 | ||||||||||
|
|
@@ -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) { | ||||||||||
|
thedavidmeister marked this conversation as resolved.
|
||||||||||
| // want to see full precision on the THREES regardless of the | ||||||||||
| // scale of the numerator and denominator. | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.