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
5 changes: 3 additions & 2 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ LibDecimalFloatPowerTest:testPowers() (gas: 1269733)
LibDecimalFloatPowerTest:testRoundTripFuzz(bytes32,bytes32) (runs: 5128, μ: 1231690, ~: 1228311)
LibDecimalFloatPowerTest:testRoundTripSimple() (gas: 1422345)
LibDecimalFloatSubTest:testSubPacked(bytes32,bytes32) (runs: 5128, μ: 8569, ~: 8256)
LibFormatDecimalFloatTest:testFormatDecimalRoundTrip(uint256) (runs: 5128, μ: 23814, ~: 18978)
LibFormatDecimalFloatTest:testFormatMem(bytes32) (runs: 5128, μ: 6767, ~: 6661)
LibFormatDecimalFloatTest:testFormatDecimalExamples() (gas: 7606)
LibFormatDecimalFloatTest:testFormatDecimalRoundTrip(uint256) (runs: 5128, μ: 24062, ~: 19314)
LibFormatDecimalFloatTest:testFormatDecimalRoundTripNegative(int256) (runs: 5128, μ: 18317, ~: 19264)
LibLogTableBytesTest:testToBytesAntiLogTableDec() (gas: 154896)
LibLogTableBytesTest:testToBytesAntiLogTableDecSmall() (gas: 159695)
LibLogTableBytesTest:testToBytesLogTableDec() (gas: 138820)
Expand Down
11 changes: 3 additions & 8 deletions src/lib/format/LibFormatDecimalFloat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ library LibFormatDecimalFloat {
/// and then formatting that as a string.
/// In the future this may be extended to support a wider range of possible
/// values.
/// @param signedCoefficient The signed coefficient of the decimal float.
/// @param exponent The exponent of the decimal float.
/// @param float The decimal float to format.
/// @return The string representation of the decimal float.
function toDecimalString(int256 signedCoefficient, int256 exponent) internal pure returns (string memory) {
function toDecimalString(Float float) internal pure returns (string memory) {
(int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(float);
string memory prefix = "";
if (signedCoefficient < 0) {
prefix = "-";
Expand All @@ -24,9 +24,4 @@ library LibFormatDecimalFloat {
uint256 decimal18Value = LibDecimalFloat.toFixedDecimalLossless(signedCoefficient, exponent, 18);
return string.concat(prefix, LibFixedPointDecimalFormat.fixedPointToDecimalString(decimal18Value));
}

function toDecimalString(Float float) internal pure returns (string memory) {
(int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(float);
return toDecimalString(signedCoefficient, exponent);
}
}
97 changes: 27 additions & 70 deletions test/src/lib/format/LibFormatDecimalFloat.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,16 @@ contract LibFormatDecimalFloatTest is Test {
using LibDecimalFloat for Float;
using LibFormatDecimalFloat for Float;

function toDecimalStringExternal(int256 signedCoefficient, int256 exponent) external pure returns (string memory) {
return LibFormatDecimalFloat.toDecimalString(signedCoefficient, exponent);
function checkFormat(int256 signedCoefficient, int256 exponent, string memory expected) internal pure {
string memory actual =
LibFormatDecimalFloat.toDecimalString(LibDecimalFloat.packLossless(signedCoefficient, exponent));
assertEq(actual, expected, "Formatted value mismatch");
}

function toString(Float float) external pure returns (string memory) {
return LibFormatDecimalFloat.toDecimalString(float);
}

/// Check that the memory version matches the stack version.
function testFormatMem(Float float) external {
(int256 signedCoefficient, int256 exponent) = float.unpack();
try this.toDecimalStringExternal(signedCoefficient, exponent) returns (string memory formatted) {
string memory actual = this.toString(float);
assertEq(formatted, actual, "Formatted value mismatch");
} catch (bytes memory err) {
vm.expectRevert(err);
LibFormatDecimalFloat.toDecimalString(float);
}
}

/// Test round tripping a value through parse and format.
function testFormatDecimalRoundTrip(uint256 value) external pure {
value = bound(value, 0, uint256(int256(type(int224).max)));
Expand All @@ -58,63 +48,30 @@ contract LibFormatDecimalFloatTest is Test {
/// Test some specific examples.
function testFormatDecimalExamples() external pure {
// pos decs
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, 0), "123456789012345678901234567890"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -1), "12345678901234567890123456789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -2), "1234567890123456789012345678.9"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -3), "123456789012345678901234567.89"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -4), "12345678901234567890123456.789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -5), "1234567890123456789012345.6789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(123456789012345678901234567890, -6), "123456789012345678901234.56789"
);
checkFormat(123456789012345678901234567890, 0, "123456789012345678901234567890");
checkFormat(123456789012345678901234567890, -1, "12345678901234567890123456789");
checkFormat(123456789012345678901234567890, -2, "1234567890123456789012345678.9");
checkFormat(123456789012345678901234567890, -3, "123456789012345678901234567.89");
checkFormat(123456789012345678901234567890, -4, "12345678901234567890123456.789");
checkFormat(123456789012345678901234567890, -5, "1234567890123456789012345.6789");
checkFormat(123456789012345678901234567890, -6, "123456789012345678901234.56789");

// zeros
assertEq(LibFormatDecimalFloat.toDecimalString(0, 0), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, -1), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, -2), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, -3), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, 1), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, 2), "0");
assertEq(LibFormatDecimalFloat.toDecimalString(0, 3), "0");
// // zeros
checkFormat(0, 0, "0");
checkFormat(0, -1, "0");
checkFormat(0, -2, "0");
checkFormat(0, -3, "0");
checkFormat(0, 1, "0");
checkFormat(0, 2, "0");
checkFormat(0, 3, "0");

// neg decs
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, 0), "-123456789012345678901234567890"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -1), "-12345678901234567890123456789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -2),
"-1234567890123456789012345678.9"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -3),
"-123456789012345678901234567.89"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -4),
"-12345678901234567890123456.789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -5),
"-1234567890123456789012345.6789"
);
assertEq(
LibFormatDecimalFloat.toDecimalString(-123456789012345678901234567890, -6),
"-123456789012345678901234.56789"
);
// // neg decs
checkFormat(-123456789012345678901234567890, 0, "-123456789012345678901234567890");
checkFormat(-123456789012345678901234567890, -1, "-12345678901234567890123456789");
checkFormat(-123456789012345678901234567890, -2, "-1234567890123456789012345678.9");
checkFormat(-123456789012345678901234567890, -3, "-123456789012345678901234567.89");
checkFormat(-123456789012345678901234567890, -4, "-12345678901234567890123456.789");
checkFormat(-123456789012345678901234567890, -5, "-1234567890123456789012345.6789");
checkFormat(-123456789012345678901234567890, -6, "-123456789012345678901234.56789");
}
}
Loading