-
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.
Merge pull request #19 from rainlanguage/2024-08-22-lossless
lossless conversion
- Loading branch information
Showing
25 changed files
with
196 additions
and
124 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: CAL | ||
pragma solidity =0.8.25; | ||
|
||
import { | ||
LibDecimalFloat, | ||
SIGNED_NORMALIZED_MAX, | ||
NORMALIZED_MAX, | ||
LossyConversionFromFloat, | ||
LossyConversionToFloat | ||
} from "../../../src/lib/LibDecimalFloat.sol"; | ||
import {Test} from "forge-std/Test.sol"; | ||
|
||
contract LibDecimalFloatDecimalLosslessTest is Test { | ||
function fromFixedDecimalLosslessExternal(uint256 value, uint8 decimals) external pure returns (int256, int256) { | ||
return LibDecimalFloat.fromFixedDecimalLossless(value, decimals); | ||
} | ||
|
||
function testToFixedDecimalLosslessPass(int256 signedCoefficient, int256 exponent, uint8 decimals) external pure { | ||
signedCoefficient = bound(signedCoefficient, 0, 1e18); | ||
exponent = bound(exponent, 0, 30); | ||
decimals = uint8(bound(decimals, 0, 18)); | ||
(uint256 expectedResult, bool success) = | ||
LibDecimalFloat.toFixedDecimalLossy(signedCoefficient, exponent, decimals); | ||
assertTrue(success, "Lossy conversion failed"); | ||
|
||
uint256 result = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, decimals); | ||
assertEq(result, expectedResult, "Lossless conversion failed"); | ||
} | ||
|
||
function testToFixedDecimalLosslessFail() external { | ||
vm.expectRevert(abi.encodeWithSelector(LossyConversionFromFloat.selector, 1, -1)); | ||
LibDecimalFloat.toFixedDecimalLossless(1, -1, 0); | ||
} | ||
|
||
function testFromFixedDecimalLosslessPass(uint256 value, uint8 decimals) external pure { | ||
value = bound(value, 0, uint256(type(int256).max)); | ||
(int256 signedCoefficient, int256 exponent) = LibDecimalFloat.fromFixedDecimalLossless(value, decimals); | ||
assertEq(uint256(signedCoefficient), value); | ||
assertEq(exponent, -int256(uint256(decimals))); | ||
} | ||
|
||
function testFromFixedDecimalLosslessFail(uint256 value, uint8 decimals) external { | ||
value = bound(value, uint256(type(int256).max) + 1, type(uint256).max); | ||
vm.assume(value % 10 != 0); | ||
vm.expectRevert( | ||
abi.encodeWithSelector(LossyConversionToFloat.selector, value / 10, 1 - int256(uint256(decimals))) | ||
); | ||
this.fromFixedDecimalLosslessExternal(value, decimals); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
test/src/LibDecimalFloat.divide.t.sol → test/src/lib/LibDecimalFloat.divide.t.sol
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
test/src/LibDecimalFloat.power.t.sol → test/src/lib/LibDecimalFloat.power.t.sol
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.