diff --git a/script/Deploy.sol b/script/Deploy.sol index ceb06b7f..6dcef5b0 100644 --- a/script/Deploy.sol +++ b/script/Deploy.sol @@ -3,7 +3,7 @@ pragma solidity =0.8.25; import {Script} from "forge-std/Script.sol"; import {DataContractMemoryContainer, LibDataContract} from "rain.datacontract/lib/LibDataContract.sol"; -import {LibDecimalFloatDeploy} from "../src/lib/LibDecimalFloatDeploy.sol"; +import {LibDecimalFloatDeploy} from "../src/lib/deploy/LibDecimalFloatDeploy.sol"; contract Deploy is Script { using LibDataContract for DataContractMemoryContainer; @@ -17,6 +17,8 @@ contract Deploy is Script { container.writeZoltu(); + LibDecimalFloatDeploy.decimalFloatZoltu(); + vm.stopBroadcast(); } } diff --git a/src/concrete/DecimalFloat.sol b/src/concrete/DecimalFloat.sol new file mode 100644 index 00000000..4026fa33 --- /dev/null +++ b/src/concrete/DecimalFloat.sol @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "../lib/LibDecimalFloat.sol"; +import {LOG_TABLES_ADDRESS} from "../lib/deploy/LibDecimalFloatDeploy.sol"; +import {LibFormatDecimalFloat} from "../lib/format/LibFormatDecimalFloat.sol"; +import {LibParseDecimalFloat} from "../lib/parse/LibParseDecimalFloat.sol"; + +contract DecimalFloat { + using LibDecimalFloat for Float; + + /// Exposes `LibParseDecimalFloat.parseDecimalFloat` for offchain use. + /// @param str The string to parse. + /// @return errorSelector The selector of the error if parsing failed. `0` + /// if parsing succeeded. + /// @return parsed The parsed float. Caller MUST check `errorSelector` to + /// determine if parsing succeeded. + function parse(string memory str) external pure returns (bytes4, Float) { + (bytes4 errorSelector, Float parsed) = LibParseDecimalFloat.parseDecimalFloat(str); + return (errorSelector, parsed); + } + + /// Exposes `LibFormatDecimalFloat.toDecimalString` for offchain use. + /// @param a The float to format. + /// @return The string representation of the float. + function format(Float a) external pure returns (string memory) { + return LibFormatDecimalFloat.toDecimalString(a); + } + + /// Exposes `LibDecimalFloat.add` for offchain use. + /// @param a The first float to add. + /// @param b The second float to add. + /// @return The sum of the two floats. + function add(Float a, Float b) external pure returns (Float) { + return a.add(b); + } + + /// Exposes `LibDecimalFloat.sub` for offchain use. + /// @param a The first float to subtract. + /// @param b The second float to subtract. + /// @return The difference of the two floats. + function sub(Float a, Float b) external pure returns (Float) { + return a.sub(b); + } + + /// Exposes `LibDecimalFloat.minus` for offchain use. + /// @param a The float to negate. + /// @return The negated float. + function minus(Float a) external pure returns (Float) { + return a.minus(); + } + + /// Exposes `LibDecimalFloat.abs` for offchain use. + /// @param a The float to get the absolute value of. + /// @return The absolute value of the float. + function abs(Float a) external pure returns (Float) { + return a.abs(); + } + + /// Exposes `LibDecimalFloat.mul` for offchain use. + /// @param a The first float to multiply. + /// @param b The second float to multiply. + /// @return The product of the two floats. + function mul(Float a, Float b) external pure returns (Float) { + return a.mul(b); + } + + /// Exposes `LibDecimalFloat.div` for offchain use. + /// @param a The first float to divide. + /// @param b The second float to divide. + /// @return The quotient of the two floats. + function div(Float a, Float b) external pure returns (Float) { + return a.div(b); + } + + /// Exposes `LibDecimalFloat.inv` for offchain use. + /// @param a The float to invert. + /// @return The inverted float. + function inv(Float a) external pure returns (Float) { + return a.inv(); + } + + /// Exposes `LibDecimalFloat.eq` for offchain use. + /// @param a The first float to compare. + /// @param b The second float to compare. + /// @return True if the two floats are equal, false otherwise. + function eq(Float a, Float b) external pure returns (bool) { + return a.eq(b); + } + + /// Exposes `LibDecimalFloat.lt` for offchain use. + /// @param a The first float to compare. + /// @param b The second float to compare. + /// @return True if the first float is less than the second, false otherwise. + function lt(Float a, Float b) external pure returns (bool) { + return a.lt(b); + } + + /// Exposes `LibDecimalFloat.gt` for offchain use. + /// @param a The first float to compare. + /// @param b The second float to compare. + /// @return True if the first float is greater than the second, false + /// otherwise. + function gt(Float a, Float b) external pure returns (bool) { + return a.gt(b); + } + + /// Exposes `LibDecimalFloat.frac` for offchain use. + /// @param a The float to get the fractional part of. + /// @return The fractional part of the float. + function frac(Float a) external pure returns (Float) { + return a.frac(); + } + + /// Exposes `LibDecimalFloat.floor` for offchain use. + /// @param a The float to get the floor of. + /// @return The floored float. + function floor(Float a) external pure returns (Float) { + return a.floor(); + } + + /// Exposes `LibDecimalFloat.pow10` for offchain use. + /// @param a The float to raise to the power of 10. + /// @return The result of raising the float to the power of 10. + function pow10(Float a) external view returns (Float) { + return a.pow10(LOG_TABLES_ADDRESS); + } + + /// Exposes `LibDecimalFloat.log10` for offchain use. + /// @param a The float to take the logarithm of. + /// @return The logarithm of the float. + function log10(Float a) external view returns (Float) { + return a.log10(LOG_TABLES_ADDRESS); + } + + /// Exposes `LibDecimalFloat.pow` for offchain use. + /// @param a The base float. + /// @param b The exponent float. + /// @return The result of raising the base float to the power of the exponent + function pow(Float a, Float b) external view returns (Float) { + return a.pow(b, LOG_TABLES_ADDRESS); + } + + /// Exposes `LibDecimalFloat.min` for offchain use. + /// @param a The first float to compare. + /// @param b The second float to compare. + /// @return The smaller of the two floats. + function min(Float a, Float b) external pure returns (Float) { + return a.min(b); + } + + /// Exposes `LibDecimalFloat.max` for offchain use. + /// @param a The first float to compare. + /// @param b The second float to compare. + /// @return The larger of the two floats. + function max(Float a, Float b) external pure returns (Float) { + return a.max(b); + } + + /// Exposes `LibDecimalFloat.isZero` for offchain use. + /// @param a The float to check. + /// @return True if the float is zero, false otherwise. + function isZero(Float a) external pure returns (bool) { + return a.isZero(); + } +} diff --git a/src/lib/LibDecimalFloat.sol b/src/lib/LibDecimalFloat.sol index d1ee3061..69e4f22b 100644 --- a/src/lib/LibDecimalFloat.sol +++ b/src/lib/LibDecimalFloat.sol @@ -418,18 +418,32 @@ library LibDecimalFloat { return result; } - /// Same as multiply, 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. + /// https://speleotrove.com/decimal/daops.html#refmult + /// > multiply takes two operands. If either operand is a special value then + /// > the general rules apply. + /// > + /// > Otherwise, the operands are multiplied together + /// > (‘long multiplication’), resulting in a number which may be as long as + /// > the sum of the lengths of the two operands, as follows: + /// > + /// > - The coefficient of the result, before rounding, is computed by + /// > multiplying together the coefficients of the operands. + /// > - The exponent of the result, before rounding, is the sum of the + /// > exponents of the two operands. + /// > - The sign of the result is the exclusive or of the signs of the + /// > operands. + /// > + /// > The result is then rounded to precision digits if necessary, counting + /// > from the most significant digit of the result. /// @param a The Float struct containing the signed coefficient and /// exponent of the first floating point number. /// @param b The Float struct containing the signed coefficient and /// exponent of the second floating point number. - function multiply(Float a, Float b) internal pure returns (Float) { + function mul(Float a, Float b) internal pure returns (Float) { (int256 signedCoefficientA, int256 exponentA) = a.unpack(); (int256 signedCoefficientB, int256 exponentB) = b.unpack(); (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.multiply(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB); (Float c, bool lossless) = packLossy(signedCoefficient, exponent); // Multiplication is typically lossless, but can be lossy in edge cases. (lossless); @@ -443,11 +457,11 @@ library LibDecimalFloat { /// exponent of the first floating point number. /// @param b The Float struct containing the signed coefficient and /// exponent of the second floating point number. - function divide(Float a, Float b) internal pure returns (Float) { + function div(Float a, Float b) internal pure returns (Float) { (int256 signedCoefficientA, int256 exponentA) = a.unpack(); (int256 signedCoefficientB, int256 exponentB) = b.unpack(); (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.divide(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB); (Float c, bool lossless) = packLossy(signedCoefficient, exponent); // Division is often lossy because it is very easy to end up with // infinite decimal representations. @@ -539,14 +553,14 @@ library LibDecimalFloat { /// Same as power10, 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 tablesDataContract The address of the contract containing the - /// logarithm tables. /// @param float The Float struct containing the signed coefficient and /// exponent of the floating point number. - function power10(address tablesDataContract, Float float) internal view returns (Float) { + /// @param tablesDataContract The address of the contract containing the + /// logarithm tables. + function pow10(Float float, address tablesDataContract) internal view returns (Float) { (int256 signedCoefficient, int256 exponent) = float.unpack(); (signedCoefficient, exponent) = - LibDecimalFloatImplementation.power10(tablesDataContract, signedCoefficient, exponent); + LibDecimalFloatImplementation.pow10(tablesDataContract, signedCoefficient, exponent); (Float result, bool lossless) = packLossy(signedCoefficient, exponent); // We don't care if power10 is lossy because it's an approximation // anyway. @@ -583,7 +597,7 @@ library LibDecimalFloat { /// @param b The float `b` in `a^b`. /// @param tablesDataContract The address of the contract containing the /// logarithm tables. - function power(Float a, Float b, address tablesDataContract) internal view returns (Float) { + function pow(Float a, Float b, address tablesDataContract) internal view returns (Float) { (int256 signedCoefficientA, int256 exponentA) = a.unpack(); (int256 signedCoefficientC, int256 exponentC) = @@ -592,10 +606,10 @@ library LibDecimalFloat { (int256 signedCoefficientB, int256 exponentB) = b.unpack(); (signedCoefficientC, exponentC) = - LibDecimalFloatImplementation.multiply(signedCoefficientC, exponentC, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.mul(signedCoefficientC, exponentC, signedCoefficientB, exponentB); (signedCoefficientC, exponentC) = - LibDecimalFloatImplementation.power10(tablesDataContract, signedCoefficientC, exponentC); + LibDecimalFloatImplementation.pow10(tablesDataContract, signedCoefficientC, exponentC); (Float c, bool lossless) = packLossy(signedCoefficientC, exponentC); // We don't care if power is lossy because it's an approximation anyway. diff --git a/src/lib/LibDecimalFloatDeploy.sol b/src/lib/deploy/LibDecimalFloatDeploy.sol similarity index 58% rename from src/lib/LibDecimalFloatDeploy.sol rename to src/lib/deploy/LibDecimalFloatDeploy.sol index 7d75712a..0853ca03 100644 --- a/src/lib/LibDecimalFloatDeploy.sol +++ b/src/lib/deploy/LibDecimalFloatDeploy.sol @@ -7,10 +7,13 @@ import { LOG_TABLES_SMALL_ALT, ANTI_LOG_TABLES, ANTI_LOG_TABLES_SMALL -} from "../generated/LogTables.pointers.sol"; +} from "../../generated/LogTables.pointers.sol"; import {LibDataContract, DataContractMemoryContainer} from "rain.datacontract/lib/LibDataContract.sol"; import {LibBytes} from "rain.solmem/lib/LibBytes.sol"; import {LibMemCpy, Pointer} from "rain.solmem/lib/LibMemCpy.sol"; +import {DecimalFloat} from "../../concrete/DecimalFloat.sol"; + +address constant LOG_TABLES_ADDRESS = 0x7A0D94F55792C434d74a40883C6ed8545E406D12; library LibDecimalFloatDeploy { function combinedTables() internal pure returns (bytes memory) { @@ -24,4 +27,18 @@ library LibDecimalFloatDeploy { LibMemCpy.unsafeCopyBytesTo(LibBytes.dataPointer(tables), pointer, tables.length); return container; } + + function decimalFloatZoltu() internal returns (DecimalFloat deployedAddress) { + //slither-disable-next-line too-many-digits + bytes memory code = type(DecimalFloat).creationCode; + bool success; + assembly ("memory-safe") { + mstore(0, 0) + success := call(gas(), 0x7A0D94F55792C434d74a40883C6ed8545E406D12, 0, add(code, 0x20), mload(code), 12, 20) + deployedAddress := mload(0) + } + if (!success) { + revert("DecimalFloat: deploy failed"); + } + } } diff --git a/src/lib/implementation/LibDecimalFloatImplementation.sol b/src/lib/implementation/LibDecimalFloatImplementation.sol index 920f3b67..f6a7d2eb 100644 --- a/src/lib/implementation/LibDecimalFloatImplementation.sol +++ b/src/lib/implementation/LibDecimalFloatImplementation.sol @@ -89,24 +89,8 @@ library LibDecimalFloatImplementation { } } - /// https://speleotrove.com/decimal/daops.html#refmult - /// > multiply takes two operands. If either operand is a special value then - /// > the general rules apply. - /// > - /// > Otherwise, the operands are multiplied together - /// > (‘long multiplication’), resulting in a number which may be as long as - /// > the sum of the lengths of the two operands, as follows: - /// > - /// > - The coefficient of the result, before rounding, is computed by - /// > multiplying together the coefficients of the operands. - /// > - The exponent of the result, before rounding, is the sum of the - /// > exponents of the two operands. - /// > - The sign of the result is the exclusive or of the signs of the - /// > operands. - /// > - /// > The result is then rounded to precision digits if necessary, counting - /// > from the most significant digit of the result. - function multiply(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) + /// Stack only implementation of `mul`. + function mul(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) internal pure returns (int256, int256) @@ -138,7 +122,7 @@ library LibDecimalFloatImplementation { if (didOverflow) { (signedCoefficientA, exponentA) = normalize(signedCoefficientA, exponentA); (signedCoefficientB, exponentB) = normalize(signedCoefficientB, exponentB); - return multiply(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + return mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB); } return (signedCoefficient, exponent); } @@ -194,7 +178,7 @@ library LibDecimalFloatImplementation { /// > The result is then rounded to precision digits, if necessary, according /// > to the rounding algorithm and taking into account the remainder from /// > the division. - function divide(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) + function div(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) internal pure returns (int256, int256) @@ -484,7 +468,7 @@ library LibDecimalFloatImplementation { // This is a negative log. i.e. log(x) where 0 < x < 1. // log(x) = -log(1/x) else { - (signedCoefficient, exponent) = divide(1e37, -37, signedCoefficient, exponent); + (signedCoefficient, exponent) = div(1e37, -37, signedCoefficient, exponent); (signedCoefficient, exponent) = log10(tablesDataContract, signedCoefficient, exponent); return minus(signedCoefficient, exponent); } @@ -502,7 +486,7 @@ library LibDecimalFloatImplementation { /// @param exponent The exponent of the floating point number. /// @return signedCoefficient The signed coefficient of the result. /// @return exponent The exponent of the result. - function power10(address tablesDataContract, int256 signedCoefficient, int256 exponent) + function pow10(address tablesDataContract, int256 signedCoefficient, int256 exponent) internal view returns (int256, int256) @@ -510,7 +494,7 @@ library LibDecimalFloatImplementation { unchecked { if (signedCoefficient < 0) { (signedCoefficient, exponent) = minus(signedCoefficient, exponent); - (signedCoefficient, exponent) = power10(tablesDataContract, signedCoefficient, exponent); + (signedCoefficient, exponent) = pow10(tablesDataContract, signedCoefficient, exponent); return inv(signedCoefficient, exponent); } @@ -849,7 +833,7 @@ library LibDecimalFloatImplementation { // (x - x1) * (y2 - y1) (numeratorSignedCoefficient, numeratorExponent) = - multiply(xDiffCoefficient0, xDiffExponent0, yDiffCoefficient, yDiffExponent); + mul(xDiffCoefficient0, xDiffExponent0, yDiffCoefficient, yDiffExponent); } // x2 - x1 @@ -857,7 +841,7 @@ library LibDecimalFloatImplementation { // ((x - x1) * (y2 - y1)) / (x2 - x1) (int256 yMarginalSignedCoefficient, int256 yMarginalExponent) = - divide(numeratorSignedCoefficient, numeratorExponent, xDiffCoefficient1, xDiffExponent1); + div(numeratorSignedCoefficient, numeratorExponent, xDiffCoefficient1, xDiffExponent1); // y1 + ((x - x1) * (y2 - y1)) / (x2 - x1) (int256 signedCoefficient, int256 exponent) = diff --git a/test/abstract/LogTest.sol b/test/abstract/LogTest.sol index 2728854c..8d06f196 100644 --- a/test/abstract/LogTest.sol +++ b/test/abstract/LogTest.sol @@ -4,7 +4,7 @@ pragma solidity =0.8.25; // Re-export console2 here for convenience. import {Test, console2} from "forge-std/Test.sol"; import {DataContractMemoryContainer, LibDataContract} from "rain.datacontract/lib/LibDataContract.sol"; -import {LibDecimalFloatDeploy} from "src/lib/LibDecimalFloatDeploy.sol"; +import {LibDecimalFloatDeploy} from "src/lib/deploy/LibDecimalFloatDeploy.sol"; abstract contract LogTest is Test { using LibDataContract for DataContractMemoryContainer; diff --git a/test/lib/LibDecimalFloatSlow.sol b/test/lib/LibDecimalFloatSlow.sol index 7de77264..4e3c69d4 100644 --- a/test/lib/LibDecimalFloatSlow.sol +++ b/test/lib/LibDecimalFloatSlow.sol @@ -7,7 +7,7 @@ import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; library LibDecimalFloatSlow { using LibDecimalFloat for Float; - function multiplySlow(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) + function mulSlow(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) internal pure returns (int256, int256) @@ -40,7 +40,7 @@ library LibDecimalFloatSlow { } function invSlow(int256 signedCoefficient, int256 exponent) internal pure returns (int256, int256) { - return LibDecimalFloatImplementation.divide(1e37, -37, signedCoefficient, exponent); + return LibDecimalFloatImplementation.div(1e37, -37, signedCoefficient, exponent); } function eqSlow(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) diff --git a/test/src/concrete/DecimalFloat.abs.t.sol b/test/src/concrete/DecimalFloat.abs.t.sol new file mode 100644 index 00000000..08c219c6 --- /dev/null +++ b/test/src/concrete/DecimalFloat.abs.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatAbsTest is Test { + using LibDecimalFloat for Float; + + function absExternal(Float a) external pure returns (Float) { + return a.abs(); + } + + function testAbsDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.absExternal(a) returns (Float b) { + Float deployedB = deployed.abs(a); + + assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.abs(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.add.t.sol b/test/src/concrete/DecimalFloat.add.t.sol new file mode 100644 index 00000000..f2294b1d --- /dev/null +++ b/test/src/concrete/DecimalFloat.add.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatAddTest is Test { + using LibDecimalFloat for Float; + + function addExternal(Float a, Float b) external pure returns (Float) { + return a.add(b); + } + + function testAddDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.addExternal(a, b) returns (Float c) { + Float deployedC = deployed.add(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.add(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.div.t.sol b/test/src/concrete/DecimalFloat.div.t.sol new file mode 100644 index 00000000..6fba4597 --- /dev/null +++ b/test/src/concrete/DecimalFloat.div.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatDivTest is Test { + using LibDecimalFloat for Float; + + function divExternal(Float a, Float b) external pure returns (Float) { + return a.div(b); + } + + function testDivDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.divExternal(a, b) returns (Float c) { + Float deployedC = deployed.div(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.div(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.eq.t.sol b/test/src/concrete/DecimalFloat.eq.t.sol new file mode 100644 index 00000000..57141fb5 --- /dev/null +++ b/test/src/concrete/DecimalFloat.eq.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatEqTest is Test { + using LibDecimalFloat for Float; + + function eqExternal(Float a, Float b) external pure returns (bool) { + return a.eq(b); + } + + function testEqDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.eqExternal(a, b) returns (bool c) { + bool deployedC = deployed.eq(a, b); + + assertEq(c, deployedC); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.eq(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.floor.t.sol b/test/src/concrete/DecimalFloat.floor.t.sol new file mode 100644 index 00000000..987f9c57 --- /dev/null +++ b/test/src/concrete/DecimalFloat.floor.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatFloorTest is Test { + using LibDecimalFloat for Float; + + function floorExternal(Float a) external pure returns (Float) { + return a.floor(); + } + + function testFloorDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.floorExternal(a) returns (Float b) { + Float deployedB = deployed.floor(a); + + assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.floor(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.format.t.sol b/test/src/concrete/DecimalFloat.format.t.sol new file mode 100644 index 00000000..b5a95f12 --- /dev/null +++ b/test/src/concrete/DecimalFloat.format.t.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; +import {LibFormatDecimalFloat} from "src/lib/format/LibFormatDecimalFloat.sol"; + +contract DecimalFloatFormatTest is Test { + using LibDecimalFloat for Float; + + function formatExternal(Float a) external pure returns (string memory) { + return LibFormatDecimalFloat.toDecimalString(a); + } + + function testFormatDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.formatExternal(a) returns (string memory str) { + string memory deployedStr = deployed.format(a); + + assertEq(str, deployedStr); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.format(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.frac.t.sol b/test/src/concrete/DecimalFloat.frac.t.sol new file mode 100644 index 00000000..eba5431b --- /dev/null +++ b/test/src/concrete/DecimalFloat.frac.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatFracTest is Test { + using LibDecimalFloat for Float; + + function fracExternal(Float a) external pure returns (Float) { + return a.frac(); + } + + function testFracDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.fracExternal(a) returns (Float b) { + Float deployedB = deployed.frac(a); + + assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.frac(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.gt.t.sol b/test/src/concrete/DecimalFloat.gt.t.sol new file mode 100644 index 00000000..fa169a12 --- /dev/null +++ b/test/src/concrete/DecimalFloat.gt.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatGtTest is Test { + using LibDecimalFloat for Float; + + function gtExternal(Float a, Float b) external pure returns (bool) { + return a.gt(b); + } + + function testGtDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.gtExternal(a, b) returns (bool c) { + bool deployedC = deployed.gt(a, b); + + assertEq(c, deployedC); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.gt(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.inv.t.sol b/test/src/concrete/DecimalFloat.inv.t.sol new file mode 100644 index 00000000..5452752e --- /dev/null +++ b/test/src/concrete/DecimalFloat.inv.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatInvTest is Test { + using LibDecimalFloat for Float; + + function invExternal(Float a) external pure returns (Float) { + return a.inv(); + } + + function testInvDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.invExternal(a) returns (Float b) { + Float deployedB = deployed.inv(a); + + assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.inv(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.isZero.t.sol b/test/src/concrete/DecimalFloat.isZero.t.sol new file mode 100644 index 00000000..f3da8a52 --- /dev/null +++ b/test/src/concrete/DecimalFloat.isZero.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatIsZeroTest is Test { + using LibDecimalFloat for Float; + + function isZeroExternal(Float a) external pure returns (bool) { + return a.isZero(); + } + + function testIsZeroDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.isZeroExternal(a) returns (bool b) { + bool deployedB = deployed.isZero(a); + + assertEq(b, deployedB); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.isZero(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.log10.t.sol b/test/src/concrete/DecimalFloat.log10.t.sol new file mode 100644 index 00000000..93ed0202 --- /dev/null +++ b/test/src/concrete/DecimalFloat.log10.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {LogTest} from "test/abstract/LogTest.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatLog10Test is LogTest { + using LibDecimalFloat for Float; + + function log10External(Float a) external returns (Float) { + return a.log10(logTables()); + } + + // function testLog10Deployed(Float a) external { + // DecimalFloat deployed = new DecimalFloat(); + + // try this.log10External(a) returns (Float b) { + // Float deployedB = deployed.log10(a); + + // assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + // } catch (bytes memory err) { + // vm.expectRevert(err); + // deployed.log10(a); + // } + // } +} diff --git a/test/src/concrete/DecimalFloat.lt.t.sol b/test/src/concrete/DecimalFloat.lt.t.sol new file mode 100644 index 00000000..ae1953de --- /dev/null +++ b/test/src/concrete/DecimalFloat.lt.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatLtTest is Test { + using LibDecimalFloat for Float; + + function ltExternal(Float a, Float b) external pure returns (bool) { + return a.lt(b); + } + + function testLtDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.ltExternal(a, b) returns (bool c) { + bool deployedC = deployed.lt(a, b); + + assertEq(c, deployedC); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.lt(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.max.t.sol b/test/src/concrete/DecimalFloat.max.t.sol new file mode 100644 index 00000000..b440a023 --- /dev/null +++ b/test/src/concrete/DecimalFloat.max.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatMaxTest is Test { + using LibDecimalFloat for Float; + + function maxExternal(Float a, Float b) external pure returns (Float) { + return a.max(b); + } + + function testMaxDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.maxExternal(a, b) returns (Float c) { + Float deployedC = deployed.max(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.max(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.min.t.sol b/test/src/concrete/DecimalFloat.min.t.sol new file mode 100644 index 00000000..8f2d083c --- /dev/null +++ b/test/src/concrete/DecimalFloat.min.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; + +contract DecimalFloatMinTest is Test { + using LibDecimalFloat for Float; + + function minExternal(Float a, Float b) external pure returns (Float) { + return a.min(b); + } + + function testMinDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.minExternal(a, b) returns (Float c) { + Float deployedC = deployed.min(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.min(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.minus.t.sol b/test/src/concrete/DecimalFloat.minus.t.sol new file mode 100644 index 00000000..68133c3c --- /dev/null +++ b/test/src/concrete/DecimalFloat.minus.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatMinusTest is Test { + using LibDecimalFloat for Float; + + function minusExternal(Float a) external pure returns (Float) { + return a.minus(); + } + + function testMinusDeployed(Float a) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.minusExternal(a) returns (Float b) { + Float deployedB = deployed.minus(a); + + assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.minus(a); + } + } +} diff --git a/test/src/concrete/DecimalFloat.mul.t.sol b/test/src/concrete/DecimalFloat.mul.t.sol new file mode 100644 index 00000000..a2dbc6bd --- /dev/null +++ b/test/src/concrete/DecimalFloat.mul.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; + +contract DecimalFloatMulTest is Test { + using LibDecimalFloat for Float; + + function mulExternal(Float a, Float b) external pure returns (Float) { + return a.mul(b); + } + + function testMulDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.mulExternal(a, b) returns (Float c) { + Float deployedC = deployed.mul(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.mul(a, b); + } + } +} diff --git a/test/src/concrete/DecimalFloat.parse.t.sol b/test/src/concrete/DecimalFloat.parse.t.sol new file mode 100644 index 00000000..d841c94a --- /dev/null +++ b/test/src/concrete/DecimalFloat.parse.t.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {LibParseDecimalFloat} from "src/lib/parse/LibParseDecimalFloat.sol"; + +contract DecimalFloatParseTest is Test { + using LibDecimalFloat for Float; + + function parseExternal(string memory str) external pure returns (bytes4, Float) { + return LibParseDecimalFloat.parseDecimalFloat(str); + } + + function testParseDeployed(string memory str) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.parseExternal(str) returns (bytes4 errorSelector, Float parsed) { + (bytes4 deployedErrorSelector, Float deployedParsed) = deployed.parse(str); + + assertEq(errorSelector, deployedErrorSelector); + assertEq(Float.unwrap(parsed), Float.unwrap(deployedParsed)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.parse(str); + } + } +} diff --git a/test/src/concrete/DecimalFloat.pow.t.sol b/test/src/concrete/DecimalFloat.pow.t.sol new file mode 100644 index 00000000..edfc9ac4 --- /dev/null +++ b/test/src/concrete/DecimalFloat.pow.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {LogTest} from "test/abstract/LogTest.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatPowTest is LogTest { + using LibDecimalFloat for Float; + + function powExternal(Float a, Float b) external returns (Float) { + return a.pow(b, logTables()); + } + + // function testPowDeployed(Float a, Float b) external { + // DecimalFloat deployed = new DecimalFloat(); + + // try this.powExternal(a, b) returns (Float c) { + // Float deployedC = deployed.pow(a, b); + + // assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + // } catch (bytes memory err) { + // vm.expectRevert(err); + // deployed.pow(a, b); + // } + // } +} diff --git a/test/src/concrete/DecimalFloat.pow10.t.sol b/test/src/concrete/DecimalFloat.pow10.t.sol new file mode 100644 index 00000000..77fdb4ea --- /dev/null +++ b/test/src/concrete/DecimalFloat.pow10.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; +import {LogTest} from "test/abstract/LogTest.sol"; + +contract DecimalFloatPow10Test is LogTest { + using LibDecimalFloat for Float; + + function pow10External(Float a) external returns (Float) { + return a.pow10(logTables()); + } + + // function testPow10Deployed(Float a) external { + // DecimalFloat deployed = new DecimalFloat(); + + // try this.pow10External(a) returns (Float b) { + // Float deployedB = deployed.pow10(a); + + // assertEq(Float.unwrap(b), Float.unwrap(deployedB)); + // } catch (bytes memory err) { + // vm.expectRevert(err); + // deployed.pow10(a); + // } + // } +} diff --git a/test/src/concrete/DecimalFloat.sub.t.sol b/test/src/concrete/DecimalFloat.sub.t.sol new file mode 100644 index 00000000..f0de67ab --- /dev/null +++ b/test/src/concrete/DecimalFloat.sub.t.sol @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; +import {DecimalFloat} from "src/concrete/DecimalFloat.sol"; + +contract DecimalFloatSubTest is Test { + using LibDecimalFloat for Float; + + function subExternal(Float a, Float b) external pure returns (Float) { + return a.sub(b); + } + + function testSubDeployed(Float a, Float b) external { + DecimalFloat deployed = new DecimalFloat(); + + try this.subExternal(a, b) returns (Float c) { + Float deployedC = deployed.sub(a, b); + + assertEq(Float.unwrap(c), Float.unwrap(deployedC)); + } catch (bytes memory err) { + vm.expectRevert(err); + deployed.sub(a, b); + } + } +} diff --git a/test/src/lib/LibDecimalFloat.divide.t.sol b/test/src/lib/LibDecimalFloat.div.t.sol similarity index 60% rename from test/src/lib/LibDecimalFloat.divide.t.sol rename to test/src/lib/LibDecimalFloat.div.t.sol index 5dbcb16d..87459c55 100644 --- a/test/src/lib/LibDecimalFloat.divide.t.sol +++ b/test/src/lib/LibDecimalFloat.div.t.sol @@ -6,39 +6,37 @@ import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFl import {Test} from "forge-std/Test.sol"; -contract LibDecimalFloatDivideTest is Test { +contract LibDecimalFloatDivTest is Test { using LibDecimalFloat for Float; - function divideExternal(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) + function divExternal(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) external pure returns (Float) { (int256 signedCoefficientC, int256 exponentC) = - LibDecimalFloatImplementation.divide(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB); (Float c, bool lossless) = LibDecimalFloat.packLossy(signedCoefficientC, exponentC); (lossless); return c; } - function divideExternal(Float floatA, Float floatB) external pure returns (Float) { - return LibDecimalFloat.divide(floatA, floatB); + function divExternal(Float floatA, Float floatB) external pure returns (Float) { + return LibDecimalFloat.div(floatA, floatB); } - function testDividePacked(Float a, Float b) external { + function testDivPacked(Float a, Float b) external { (int256 signedCoefficientA, int256 exponentA) = a.unpack(); (int256 signedCoefficientB, int256 exponentB) = b.unpack(); - try this.divideExternal(signedCoefficientA, exponentA, signedCoefficientB, exponentB) returns ( - Float resultParts - ) { + try this.divExternal(signedCoefficientA, exponentA, signedCoefficientB, exponentB) returns (Float resultParts) { (int256 signedCoefficient, int256 exponent) = LibDecimalFloat.unpack(resultParts); - Float float = this.divideExternal(a, b); + Float float = this.divExternal(a, b); (int256 signedCoefficientFloat, int256 exponentFloat) = float.unpack(); assertEq(signedCoefficient, signedCoefficientFloat); assertEq(exponent, exponentFloat); } catch (bytes memory err) { vm.expectRevert(err); - this.divideExternal(a, b); + this.divExternal(a, b); } } } diff --git a/test/src/lib/LibDecimalFloat.isZero.t.sol b/test/src/lib/LibDecimalFloat.isZero.t.sol new file mode 100644 index 00000000..fb276671 --- /dev/null +++ b/test/src/lib/LibDecimalFloat.isZero.t.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: CAL +pragma solidity =0.8.25; + +import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; +import {Test} from "forge-std/Test.sol"; + +contract LibDecimalFloatIsZeroTest is Test { + using LibDecimalFloat for Float; + + function isZeroExternal(Float a) external pure returns (bool) { + return a.isZero(); + } + + function testIsZeroDeployed(Float a) external { + try this.isZeroExternal(a) returns (bool b) { + bool deployedB = a.isZero(); + + assertEq(b, deployedB); + } catch (bytes memory err) { + vm.expectRevert(err); + a.isZero(); + } + } + + function testIsZeroEqZero(Float a) external pure { + assertEq(a.isZero(), a.eq(Float.wrap(0))); + } + + function testIsZeroExamples(int32 exponent) external pure { + Float zero = Float.wrap(0); + Float packZeroBasic = LibDecimalFloat.packLossless(0, 0); + Float packZero = LibDecimalFloat.packLossless(0, exponent); + + assertTrue(zero.isZero()); + assertTrue(packZeroBasic.isZero()); + assertTrue(packZero.isZero()); + } + + function testNotIsZero(int224 signedCoefficient, int32 exponent) external pure { + vm.assume(signedCoefficient != 0); + Float notZero = LibDecimalFloat.packLossless(signedCoefficient, exponent); + assertTrue(!notZero.isZero()); + } +} diff --git a/test/src/lib/LibDecimalFloat.mixed.t.sol b/test/src/lib/LibDecimalFloat.mixed.t.sol index d9b7bdf1..51637e0f 100644 --- a/test/src/lib/LibDecimalFloat.mixed.t.sol +++ b/test/src/lib/LibDecimalFloat.mixed.t.sol @@ -10,15 +10,15 @@ contract LibDecimalFloatMixedTest is Test { using LibDecimalFloat for Float; /// (1 / 3) * 555e18 - function testDivide1Over3() external pure { + function testDiv1Over3() external pure { Float a = LibDecimalFloat.packLossless(1, 0); Float b = LibDecimalFloat.packLossless(3, 0); - Float c = a.divide(b); + Float c = a.div(b); (int256 signedCoefficientDiv, int256 exponentDiv) = LibDecimalFloat.unpack(c); assertEq(signedCoefficientDiv, THREES, "coefficient"); assertEq(exponentDiv, -38, "exponent"); - Float d = c.multiply(LibDecimalFloat.packLossless(555, 18)); + Float d = c.mul(LibDecimalFloat.packLossless(555, 18)); (int256 signedCoefficientMul, int256 exponentMul) = LibDecimalFloat.unpack(d); assertEq(signedCoefficientMul, 18499999999999999999999999999999999999815); diff --git a/test/src/lib/LibDecimalFloat.multiply.t.sol b/test/src/lib/LibDecimalFloat.mul.t.sol similarity index 60% rename from test/src/lib/LibDecimalFloat.multiply.t.sol rename to test/src/lib/LibDecimalFloat.mul.t.sol index 538621e9..e9f610ea 100644 --- a/test/src/lib/LibDecimalFloat.multiply.t.sol +++ b/test/src/lib/LibDecimalFloat.mul.t.sol @@ -6,39 +6,38 @@ import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFl import {Test} from "forge-std/Test.sol"; -contract LibDecimalFloatMultiplyTest is Test { +contract LibDecimalFloatMulTest is Test { using LibDecimalFloat for Float; - function multiplyExternal(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) + function mulExternal(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) external pure returns (Float) { (int256 signedCoefficientC, int256 exponentC) = - LibDecimalFloatImplementation.multiply(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB); (Float c, bool lossless) = LibDecimalFloat.packLossy(signedCoefficientC, exponentC); (lossless); return c; } - function multiplyExternal(Float floatA, Float floatB) external pure returns (Float) { - return LibDecimalFloat.multiply(floatA, floatB); + function mulExternal(Float floatA, Float floatB) external pure returns (Float) { + return LibDecimalFloat.mul(floatA, floatB); } - function testMultiplyPacked(Float a, Float b) external { + function testMulPacked(Float a, Float b) external { (int256 signedCoefficientA, int256 exponentA) = a.unpack(); (int256 signedCoefficientB, int256 exponentB) = b.unpack(); - try this.multiplyExternal(signedCoefficientA, exponentA, signedCoefficientB, exponentB) returns ( - Float floatExternal - ) { + try this.mulExternal(signedCoefficientA, exponentA, signedCoefficientB, exponentB) returns (Float floatExternal) + { (int256 signedCoefficient, int256 exponent) = floatExternal.unpack(); - Float float = this.multiplyExternal(a, b); + Float float = this.mulExternal(a, b); (int256 signedCoefficientUnpacked, int256 exponentUnpacked) = float.unpack(); assertEq(signedCoefficient, signedCoefficientUnpacked); assertEq(exponent, exponentUnpacked); } catch (bytes memory err) { vm.expectRevert(err); - this.multiplyExternal(a, b); + this.mulExternal(a, b); } } } diff --git a/test/src/lib/LibDecimalFloat.power.t.sol b/test/src/lib/LibDecimalFloat.pow.t.sol similarity index 74% rename from test/src/lib/LibDecimalFloat.power.t.sol rename to test/src/lib/LibDecimalFloat.pow.t.sol index afec41a1..a4fed971 100644 --- a/test/src/lib/LibDecimalFloat.power.t.sol +++ b/test/src/lib/LibDecimalFloat.pow.t.sol @@ -7,14 +7,14 @@ import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; import {console2} from "forge-std/Test.sol"; -contract LibDecimalFloatPowerTest is LogTest { +contract LibDecimalFloatPowTest is LogTest { using LibDecimalFloat for Float; function diffLimit() internal pure returns (Float) { return LibDecimalFloat.packLossless(94, -3); } - function checkPower( + function checkPow( int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, @@ -26,7 +26,7 @@ contract LibDecimalFloatPowerTest is LogTest { Float b = LibDecimalFloat.packLossless(signedCoefficientB, exponentB); address tables = logTables(); uint256 beforeGas = gasleft(); - Float c = a.power(b, tables); + Float c = a.pow(b, tables); uint256 afterGas = gasleft(); console2.log("Gas used:", beforeGas - afterGas); console2.logInt(signedCoefficientA); @@ -36,12 +36,12 @@ contract LibDecimalFloatPowerTest is LogTest { assertEq(actualExponent, expectedExponent, "exponent"); } - function testPowers() external { - checkPower(5e37, -38, 3e37, -36, 9.3283582089552238805970149253731343283e37, -47); - checkPower(5e37, -38, 6e37, -36, 8.7108013937282229965156794425087108013e37, -56); + function testPows() external { + checkPow(5e37, -38, 3e37, -36, 9.3283582089552238805970149253731343283e37, -47); + checkPow(5e37, -38, 6e37, -36, 8.7108013937282229965156794425087108013e37, -56); // // Issues found in fuzzing from here. - checkPower(99999, 0, 12182, 0, 1000, 60907); - checkPower(1785215562, 0, 18, 0, 3388, 163); + checkPow(99999, 0, 12182, 0, 1000, 60907); + checkPow(1785215562, 0, 18, 0, 3388, 163); } function checkRoundTrip(int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, int256 exponentB) @@ -50,11 +50,11 @@ contract LibDecimalFloatPowerTest is LogTest { Float a = LibDecimalFloat.packLossless(signedCoefficientA, exponentA); Float b = LibDecimalFloat.packLossless(signedCoefficientB, exponentB); address tables = logTables(); - Float c = a.power(b, tables); + Float c = a.pow(b, tables); - Float roundTrip = c.power(b.inv(), tables); + Float roundTrip = c.pow(b.inv(), tables); - Float diff = a.divide(roundTrip).sub(LibDecimalFloat.packLossless(1, 0)).abs(); + Float diff = a.div(roundTrip).sub(LibDecimalFloat.packLossless(1, 0)).abs(); assertTrue(!diff.gt(diffLimit()), "diff"); } @@ -73,20 +73,20 @@ contract LibDecimalFloatPowerTest is LogTest { checkRoundTrip(4157, 0, -1, -2); } - function powerExternal(Float a, Float b) external returns (Float) { - return a.power(b, logTables()); + function powExternal(Float a, Float b) external returns (Float) { + return a.pow(b, logTables()); } function testRoundTripFuzz(Float a, Float b) external { - try this.powerExternal(a, b) returns (Float c) { + try this.powExternal(a, b) returns (Float c) { // If b is zero we'll divide by zero on the inv. // If c is 1 then it's not round trippable because 1^x = 1 for all x. // C will be 1 when a is 1 or b is 0 (or very close to either). if (b.isZero() || c.eq(LibDecimalFloat.packLossless(1, 0))) {} else { Float inv = b.inv(); - try this.powerExternal(c, inv) returns (Float roundTrip) { + try this.powExternal(c, inv) returns (Float roundTrip) { if (roundTrip.isZero()) {} else { - Float diff = a.divide(roundTrip).sub(LibDecimalFloat.packLossless(1, 0)).abs(); + Float diff = a.div(roundTrip).sub(LibDecimalFloat.packLossless(1, 0)).abs(); assertTrue(!diff.gt(diffLimit()), "diff"); } } catch (bytes memory err) {} diff --git a/test/src/lib/LibDecimalFloat.power10.t.sol b/test/src/lib/LibDecimalFloat.pow10.t.sol similarity index 59% rename from test/src/lib/LibDecimalFloat.power10.t.sol rename to test/src/lib/LibDecimalFloat.pow10.t.sol index 342f3965..a12fca05 100644 --- a/test/src/lib/LibDecimalFloat.power10.t.sol +++ b/test/src/lib/LibDecimalFloat.pow10.t.sol @@ -5,34 +5,34 @@ import {LibDecimalFloat, Float, ExponentOverflow} from "src/lib/LibDecimalFloat. import {LogTest} from "../../abstract/LogTest.sol"; import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFloatImplementation.sol"; -contract LibDecimalFloatPower10Test is LogTest { +contract LibDecimalFloatPow10Test is LogTest { using LibDecimalFloat for Float; - function power10External(int256 signedCoefficient, int256 exponent) external returns (int256, int256) { - return LibDecimalFloatImplementation.power10(logTables(), signedCoefficient, exponent); + function pow10External(int256 signedCoefficient, int256 exponent) external returns (int256, int256) { + return LibDecimalFloatImplementation.pow10(logTables(), signedCoefficient, exponent); } - function power10External(Float float) external returns (Float) { - return LibDecimalFloat.power10(logTables(), float); + function pow10External(Float float) external returns (Float) { + return LibDecimalFloat.pow10(float, logTables()); } - function testPower10Packed(Float float) external { + function testPow10Packed(Float float) external { (int256 signedCoefficientFloat, int256 exponentFloat) = float.unpack(); - try this.power10External(signedCoefficientFloat, exponentFloat) returns ( + try this.pow10External(signedCoefficientFloat, exponentFloat) returns ( int256 signedCoefficient, int256 exponent ) { if (exponent > type(int32).max) { vm.expectRevert(abi.encodeWithSelector(ExponentOverflow.selector, signedCoefficient, exponent)); - Float floatPower10 = this.power10External(float); + Float floatPower10 = this.pow10External(float); } else { - Float floatPower10 = this.power10External(float); + Float floatPower10 = this.pow10External(float); (int256 signedCoefficientUnpacked, int256 exponentUnpacked) = floatPower10.unpack(); assertEq(signedCoefficient, signedCoefficientUnpacked); assertEq(exponent, exponentUnpacked); } } catch (bytes memory err) { vm.expectRevert(err); - this.power10External(float); + this.pow10External(float); } } } diff --git a/test/src/lib/implementation/LibDecimalFloatImplementation.divide.t.sol b/test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol similarity index 53% rename from test/src/lib/implementation/LibDecimalFloatImplementation.divide.t.sol rename to test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol index 622ad857..ebb4f65f 100644 --- a/test/src/lib/implementation/LibDecimalFloatImplementation.divide.t.sol +++ b/test/src/lib/implementation/LibDecimalFloatImplementation.div.t.sol @@ -9,8 +9,8 @@ import { } from "src/lib/implementation/LibDecimalFloatImplementation.sol"; import {THREES, ONES} from "../../../lib/LibCommonResults.sol"; -contract LibDecimalFloatImplementationDivideTest is Test { - function checkDivision( +contract LibDecimalFloatImplementationDivTest is Test { + function checkDiv( int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, @@ -19,82 +19,82 @@ contract LibDecimalFloatImplementationDivideTest is Test { int256 exponentC ) internal pure { (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.divide(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB); assertEq(signedCoefficient, signedCoefficientC, "coefficient"); assertEq(exponent, exponentC, "exponent"); } /// 1 / 3 gas by parts 10 - function testDivide1Over3Gas10() external pure { - (int256 c, int256 e) = LibDecimalFloatImplementation.divide(1, 0, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); - (c, e) = LibDecimalFloatImplementation.divide(c, e, 3e37, -37); + function testDiv1Over3Gas10() external pure { + (int256 c, int256 e) = LibDecimalFloatImplementation.div(1, 0, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); + (c, e) = LibDecimalFloatImplementation.div(c, e, 3e37, -37); } /// 1 / 3 - function testDivide1Over3() external pure { - checkDivision(1, 0, 3, 0, THREES, -38); + function testDiv1Over3() external pure { + checkDiv(1, 0, 3, 0, THREES, -38); } /// - 1 / 3 - function testDivideNegative1Over3() external pure { - checkDivision(-1, 0, 3, 0, -THREES, -38); + function testDivNegative1Over3() external pure { + checkDiv(-1, 0, 3, 0, -THREES, -38); } /// 1 / 3 gas - function testDivide1Over3Gas0() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.divide(1e37, -37, 3e37, -37); + function testDiv1Over3Gas0() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.div(1e37, -37, 3e37, -37); (signedCoefficient, exponent); } /// 1e18 / 3 - function testDivide1e18Over3() external pure { - checkDivision(1e18, 0, 3, 0, THREES, -20); + function testDiv1e18Over3() external pure { + checkDiv(1e18, 0, 3, 0, THREES, -20); } /// 10,0 / 1e38,-37 == 1 - function testDivideTenOverOOMs() external pure { - checkDivision(10, 0, 1e38, -37, 1e38, -38); + function testDivTenOverOOMs() external pure { + checkDiv(10, 0, 1e38, -37, 1e38, -38); } /// 1e38,-37 / 2,0 == 5 - function testDivideOOMsOverTen() external pure { - checkDivision(1e38, -37, 2, 0, 5e37, -37); + function testDivOOMsOverTen() external pure { + checkDiv(1e38, -37, 2, 0, 5e37, -37); } /// 5e37,-37 / 2e37,-37 == 2.5 - function testDivideOOMs5and2() external pure { - checkDivision(5e37, -37, 2e37, -37, 25e37, -38); + function testDivOOMs5and2() external pure { + checkDiv(5e37, -37, 2e37, -37, 25e37, -38); } /// (1 / 9) / (1 / 3) == 0.333.. - function testDivide1Over9Over1Over3() external pure { + function testDiv1Over9Over1Over3() external pure { // 1 / 9 - (int256 signedCoefficientA, int256 exponentA) = LibDecimalFloatImplementation.divide(1, 0, 9, 0); + (int256 signedCoefficientA, int256 exponentA) = LibDecimalFloatImplementation.div(1, 0, 9, 0); assertEq(signedCoefficientA, ONES); assertEq(exponentA, -38); // 1 / 3 - (int256 signedCoefficientB, int256 exponentB) = LibDecimalFloatImplementation.divide(1, 0, 3, 0); + (int256 signedCoefficientB, int256 exponentB) = LibDecimalFloatImplementation.div(1, 0, 3, 0); assertEq(signedCoefficientB, THREES); assertEq(exponentB, -38); // (1 / 9) / (1 / 3) (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.divide(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.div(signedCoefficientA, exponentA, signedCoefficientB, exponentB); assertEq(signedCoefficient, THREES); assertEq(exponent, -38); } /// forge-config: default.fuzz.runs = 100 - function testUnnormalizedThreesDivision0(int256 exponentA, int256 exponentB) external pure { + function testUnnormalizedThreesDiv0(int256 exponentA, int256 exponentB) external pure { exponentA = bound(exponentA, EXPONENT_MIN / 2, EXPONENT_MAX / 2); exponentB = bound(exponentB, EXPONENT_MIN / 2, EXPONENT_MAX / 2); @@ -106,7 +106,7 @@ contract LibDecimalFloatImplementationDivideTest is Test { while (true) { // want to see full precision on the THREES regardless of the // scale of the numerator and denominator. - checkDivision(i, exponentA, d, exponentB, THREES, exponentA - exponentB + j); + checkDiv(i, exponentA, d, exponentB, THREES, exponentA - exponentB + j); if (i == 1e76) { break; diff --git a/test/src/lib/implementation/LibDecimalFloatImplementation.multiply.t.sol b/test/src/lib/implementation/LibDecimalFloatImplementation.mul.t.sol similarity index 67% rename from test/src/lib/implementation/LibDecimalFloatImplementation.multiply.t.sol rename to test/src/lib/implementation/LibDecimalFloatImplementation.mul.t.sol index 393c9ffd..d3461511 100644 --- a/test/src/lib/implementation/LibDecimalFloatImplementation.multiply.t.sol +++ b/test/src/lib/implementation/LibDecimalFloatImplementation.mul.t.sol @@ -11,85 +11,85 @@ import { import {Test} from "forge-std/Test.sol"; import {LibDecimalFloatSlow} from "test/lib/LibDecimalFloatSlow.sol"; -contract LibDecimalFloatImplementationMultiplyTest is Test { +contract LibDecimalFloatImplementationMulTest is Test { /// Simple 0 multiply 0 /// 0 * 0 = 0 - function testMultiplyZero0Exponent() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(0, 0, 0, 0); + function testMulZero0Exponent() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(0, 0, 0, 0); assertEq(signedCoefficient, NORMALIZED_ZERO_SIGNED_COEFFICIENT); assertEq(exponent, NORMALIZED_ZERO_EXPONENT); } /// 0 multiply 0 any exponent /// 0 * 0 = 0 - function testMultiplyZeroAnyExponent(int64 exponentA, int64 exponentB) external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(0, exponentA, 0, exponentB); + function testMulZeroAnyExponent(int64 exponentA, int64 exponentB) external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(0, exponentA, 0, exponentB); assertEq(signedCoefficient, NORMALIZED_ZERO_SIGNED_COEFFICIENT); assertEq(exponent, NORMALIZED_ZERO_EXPONENT); } /// 0 multiply 1 /// 0 * 1 = 0 - function testMultiplyZeroOne() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(0, 0, 1, 0); + function testMulZeroOne() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(0, 0, 1, 0); assertEq(signedCoefficient, NORMALIZED_ZERO_SIGNED_COEFFICIENT); assertEq(exponent, NORMALIZED_ZERO_EXPONENT); } /// 1 multiply 0 /// 1 * 0 = 0 - function testMultiplyOneZero() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(1, 0, 0, 0); + function testMulOneZero() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(1, 0, 0, 0); assertEq(signedCoefficient, NORMALIZED_ZERO_SIGNED_COEFFICIENT); assertEq(exponent, NORMALIZED_ZERO_EXPONENT); } /// 1 multiply 1 /// 1 * 1 = 1 - function testMultiplyOneOne() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(1, 0, 1, 0); + function testMulOneOne() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(1, 0, 1, 0); assertEq(signedCoefficient, 1); assertEq(exponent, 0); } /// 123456789 multiply 987654321 /// 123456789 * 987654321 = 121932631112635269 - function testMultiply123456789987654321() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(123456789, 0, 987654321, 0); + function testMul123456789987654321() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(123456789, 0, 987654321, 0); assertEq(signedCoefficient, 121932631112635269); assertEq(exponent, 0); } /// 123456789 multiply 987654321 with exponents /// 123456789 * 987654321 = 121932631112635269 - function testMultiply123456789987654321WithExponents(int128 exponentA, int128 exponentB) external pure { + function testMul123456789987654321WithExponents(int128 exponentA, int128 exponentB) external pure { exponentA = int128(bound(exponentA, -127, 127)); exponentB = int128(bound(exponentB, -127, 127)); (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.multiply(123456789, exponentA, 987654321, exponentB); + LibDecimalFloatImplementation.mul(123456789, exponentA, 987654321, exponentB); assertEq(signedCoefficient, 121932631112635269); assertEq(exponent, exponentA + exponentB); } /// 1e18 * 1e-19 = 1e-1 - function testMultiply1e181e19() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(1, 18, 1, -19); + function testMul1e181e19() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(1, 18, 1, -19); assertEq(signedCoefficient, 1); assertEq(exponent, -1); } - function testMultiplyGasZero() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(0, 0, 0, 0); + function testMulGasZero() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(0, 0, 0, 0); (signedCoefficient, exponent); } - function testMultiplyGasOne() external pure { - (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.multiply(1e37, -37, 1e37, -37); + function testMulGasOne() external pure { + (int256 signedCoefficient, int256 exponent) = LibDecimalFloatImplementation.mul(1e37, -37, 1e37, -37); (signedCoefficient, exponent); } - function testMultiplyNotRevertAnyExpectation( + function testMulNotRevertAnyExpectation( int256 signedCoefficientA, int256 exponentA, int256 signedCoefficientB, @@ -98,9 +98,9 @@ contract LibDecimalFloatImplementationMultiplyTest is Test { exponentA = bound(exponentA, EXPONENT_MIN, EXPONENT_MAX); exponentB = bound(exponentB, EXPONENT_MIN, EXPONENT_MAX); (int256 signedCoefficient, int256 exponent) = - LibDecimalFloatImplementation.multiply(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatImplementation.mul(signedCoefficientA, exponentA, signedCoefficientB, exponentB); (int256 expectedSignedCoefficient, int256 expectedExponent) = - LibDecimalFloatSlow.multiplySlow(signedCoefficientA, exponentA, signedCoefficientB, exponentB); + LibDecimalFloatSlow.mulSlow(signedCoefficientA, exponentA, signedCoefficientB, exponentB); assertEq(signedCoefficient, expectedSignedCoefficient); assertEq(exponent, expectedExponent); diff --git a/test/src/lib/implementation/LibDecimalFloatImplementation.power10.t.sol b/test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol similarity index 60% rename from test/src/lib/implementation/LibDecimalFloatImplementation.power10.t.sol rename to test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol index 0c511436..fbc14173 100644 --- a/test/src/lib/implementation/LibDecimalFloatImplementation.power10.t.sol +++ b/test/src/lib/implementation/LibDecimalFloatImplementation.pow10.t.sol @@ -6,10 +6,10 @@ import {LogTest} from "../../../abstract/LogTest.sol"; import {LibDecimalFloatImplementation} from "src/lib/implementation/LibDecimalFloatImplementation.sol"; import {LibDecimalFloat, Float} from "src/lib/LibDecimalFloat.sol"; -contract LibDecimalFloatImplementationPower10Test is LogTest { +contract LibDecimalFloatImplementationPow10Test is LogTest { using LibDecimalFloat for Float; - function checkPower10( + function checkPow10( int256 signedCoefficient, int256 exponent, int256 expectedSignedCoefficient, @@ -17,55 +17,55 @@ contract LibDecimalFloatImplementationPower10Test is LogTest { ) internal { address tables = logTables(); (int256 actualSignedCoefficient, int256 actualExponent) = - LibDecimalFloatImplementation.power10(tables, signedCoefficient, exponent); + LibDecimalFloatImplementation.pow10(tables, signedCoefficient, exponent); assertEq(actualSignedCoefficient, expectedSignedCoefficient, "signedCoefficient"); assertEq(actualExponent, expectedExponent, "exponent"); } - function testExactPowers() external { + function testExactPows() external { // 10^1 = 10 - checkPower10(1e37, -37, 1000, -2); + checkPow10(1e37, -37, 1000, -2); // 10^10 = 10000000000 - checkPower10(10e37, -37, 1000, 7); - checkPower10(1, 2, 1000, 97); - checkPower10(1, 3, 1000, 997); - checkPower10(1, 4, 1000, 9997); + checkPow10(10e37, -37, 1000, 7); + checkPow10(1, 2, 1000, 97); + checkPow10(1, 3, 1000, 997); + checkPow10(1, 4, 1000, 9997); } function testExactLookups() external { // 10^2 = 100 - checkPower10(2, 0, 1000, -1); + checkPow10(2, 0, 1000, -1); // 10^3 = 1000 - checkPower10(3, 0, 1000, 0); + checkPow10(3, 0, 1000, 0); // 10^4 = 10000 - checkPower10(4, 0, 1000, 1); + checkPow10(4, 0, 1000, 1); // 10^5 = 100000 - checkPower10(5, 0, 1000, 2); + checkPow10(5, 0, 1000, 2); // 10^6 = 1000000 - checkPower10(6, 0, 1000, 3); + checkPow10(6, 0, 1000, 3); // 10^7 = 10000000 - checkPower10(7, 0, 1000, 4); + checkPow10(7, 0, 1000, 4); // 10^8 = 100000000 - checkPower10(8, 0, 1000, 5); + checkPow10(8, 0, 1000, 5); // 10^9 = 1000000000 - checkPower10(9, 0, 1000, 6); + checkPow10(9, 0, 1000, 6); // 10^1.5 = 31.622776601683793319988935444327074859 - checkPower10(1.5e37, -37, 3162, -2); + checkPow10(1.5e37, -37, 3162, -2); - checkPower10(0.5e37, -37, 3162, -3); + checkPow10(0.5e37, -37, 3162, -3); - checkPower10(0.3e37, -37, 1995, -3); - checkPower10(-0.3e37, -37, 5.012531328320802005012531328320802005e37, -38); + checkPow10(0.3e37, -37, 1995, -3); + checkPow10(-0.3e37, -37, 5.012531328320802005012531328320802005e37, -38); } function testInterpolatedLookupsPower() external { // 10^1.55555 = 35.9376769153 - checkPower10(1.55555e37, -37, 35935e37, -40); + checkPow10(1.55555e37, -37, 35935e37, -40); // 10^1234.56789 - checkPower10(123456789, -5, 36979e37, 1193); + checkPow10(123456789, -5, 36979e37, 1193); // ~= 10 (fuzzing found this edge case). - checkPower10(99999999999999999999999999999999999997448, -41, 99999999999999999999999999999999999991000, -40); + checkPow10(99999999999999999999999999999999999997448, -41, 99999999999999999999999999999999999991000, -40); } function boundFloat(int224 x, int32 exponent) internal pure returns (int224, int32) { @@ -79,6 +79,6 @@ contract LibDecimalFloatImplementationPower10Test is LogTest { /// Test the current range that we can handle power10 over does not revert. function testNoRevert(int224 x, int32 exponent) external { (x, exponent) = boundFloat(x, exponent); - LibDecimalFloatImplementation.power10(logTables(), x, exponent); + LibDecimalFloatImplementation.pow10(logTables(), x, exponent); } }