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
10 changes: 10 additions & 0 deletions src/lib/LibDecimalFloat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1176,4 +1176,14 @@ library LibDecimalFloat {
(result.signedCoefficient, result.exponent) =
max(a.signedCoefficient, a.exponent, b.signedCoefficient, b.exponent);
}

/// Same as normalize, but accepts a Float struct instead of separate values.
/// Costs more gas but helps mitigate stack depth issues, and is more
/// ergonomic for the caller.
/// @param float The Float struct containing the signed coefficient and
/// exponent of the floating point number.
function normalize(Float memory float) internal pure returns (Float memory result) {
(result.signedCoefficient, result.exponent) =
LibDecimalFloatImplementation.normalize(float.signedCoefficient, float.exponent);
}
}
32 changes: 32 additions & 0 deletions test/src/lib/LibDecimalFloat.normalize.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;

import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol";
import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFloatImplementation.sol";
import {Test} from "forge-std/Test.sol";

contract LibDecimalFloatNormalizeTest is Test {
using LibDecimalFloat for Float;

function normalizeExternal(int256 signedCoefficient, int256 exponent) external pure returns (int256, int256) {
return LibDecimalFloatImplementation.normalize(signedCoefficient, exponent);
}

function normalizeExternal(Float memory float) external pure returns (Float memory) {
return LibDecimalFloat.normalize(float);
}
/// Stack and mem are the same.

function testNormalizeMem(Float memory float) external {
try this.normalizeExternal(float.signedCoefficient, float.exponent) returns (
int256 signedCoefficient, int256 exponent
) {
Float memory floatNormalized = this.normalizeExternal(float);
assertEq(signedCoefficient, floatNormalized.signedCoefficient);
assertEq(exponent, floatNormalized.exponent);
} catch (bytes memory err) {
vm.expectRevert(err);
this.normalizeExternal(float);
}
}
}
2 changes: 1 addition & 1 deletion test/src/lib/LibDecimalFloat.pack.t.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.25;
pragma solidity =0.8.25;

import {
LibDecimalFloat,
Expand Down
Loading