-
Notifications
You must be signed in to change notification settings - Fork 2
pack 0 standardized #136
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
pack 0 standardized #136
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,6 +22,30 @@ contract LibDecimalFloatPackTest is Test { | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| assertTrue(lossless, "lossless"); | ||||||||||||||||||||||||||||
| assertEq(signedCoefficient, signedCoefficientOut, "coefficient"); | ||||||||||||||||||||||||||||
| assertEq(exponent, exponentOut, "exponent"); | ||||||||||||||||||||||||||||
| assertEq(signedCoefficient == 0 ? int256(0) : exponent, exponentOut, "exponent"); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /// Packing 0 is always lossless and returns standard zero float. | ||||||||||||||||||||||||||||
| function testPackZero(int256 exponent) external pure { | ||||||||||||||||||||||||||||
| (Float float, bool lossless) = LibDecimalFloat.packLossy(0, exponent); | ||||||||||||||||||||||||||||
| assertTrue(lossless, "lossless"); | ||||||||||||||||||||||||||||
| assertEq(Float.unwrap(float), Float.unwrap(LibDecimalFloat.FLOAT_ZERO), "float"); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
thedavidmeister marked this conversation as resolved.
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /// Error when exponent larger than int32.max except for zero. | ||||||||||||||||||||||||||||
| function testPackExponentOverflow(int256 signedCoefficient, int256 exponent) external { | ||||||||||||||||||||||||||||
| exponent = bound(exponent, int256(type(int32).max) + 1, type(int256).max - 77); | ||||||||||||||||||||||||||||
| vm.assume(signedCoefficient != 0); | ||||||||||||||||||||||||||||
| vm.expectRevert(abi.encodeWithSelector(ExponentOverflow.selector, signedCoefficient, exponent)); | ||||||||||||||||||||||||||||
| this.packLossyExternal(signedCoefficient, exponent); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
thedavidmeister marked this conversation as resolved.
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /// Lossy zero when exponent is negative below type(int32).min except for zero. | ||||||||||||||||||||||||||||
| function testPackNegativeExponentLossyZero(int256 signedCoefficient, int256 exponent) external view { | ||||||||||||||||||||||||||||
| exponent = bound(exponent, type(int256).min, int256(type(int32).min) - 77); | ||||||||||||||||||||||||||||
| vm.assume(signedCoefficient != 0); | ||||||||||||||||||||||||||||
| (Float float, bool lossless) = this.packLossyExternal(signedCoefficient, exponent); | ||||||||||||||||||||||||||||
| assertFalse(lossless, "lossless"); | ||||||||||||||||||||||||||||
| assertEq(Float.unwrap(float), Float.unwrap(LibDecimalFloat.FLOAT_ZERO), "float"); | ||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+49
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 Tighten the underflow test: make it pure and target the threshold precisely.
Apply this diff: - function testPackNegativeExponentLossyZero(int256 signedCoefficient, int256 exponent) external view {
- exponent = bound(exponent, type(int256).min, int256(type(int32).min) - 77);
- vm.assume(signedCoefficient != 0);
+ function testPackNegativeExponentLossyZero(int256 signedCoefficient, int256 exponent) external pure {
+ signedCoefficient = bound(signedCoefficient, 1, type(int256).max);
+ exponent = bound(exponent, int256(type(int32).min) - 77, int256(type(int32).min) - 77);
(Float float, bool lossless) = this.packLossyExternal(signedCoefficient, exponent);
assertFalse(lossless, "lossless");
assertEq(Float.unwrap(float), Float.unwrap(LibDecimalFloat.FLOAT_ZERO), "float");
}Consider adding a sibling test for the “within 1..76” underflow window to assert non‑zero, lossy pack clamped at 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.