Skip to content
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

fix: threshold check in SD59x18 exp2 function #228

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/sd59x18/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ SD59x18 constant EXP_MAX_INPUT = SD59x18.wrap(uEXP_MAX_INPUT);
int256 constant uEXP2_MAX_INPUT = 192e18 - 1;
SD59x18 constant EXP2_MAX_INPUT = SD59x18.wrap(uEXP2_MAX_INPUT);

/// @dev Any value less than this will return 0 in {exp2}.
int256 constant uEXP2_MIN_THRESHOLD = -59_794705707972522261;
SD59x18 constant EXP2_MIN_THRESHOLD = SD59x18.wrap(uEXP2_MIN_THRESHOLD);

/// @dev Half the UNIT number.
int256 constant uHALF_UNIT = 0.5e18;
SD59x18 constant HALF_UNIT = SD59x18.wrap(uHALF_UNIT);
Expand Down
6 changes: 5 additions & 1 deletion src/sd59x18/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./Errors.sol" as Errors;
import {
uEXP_MAX_INPUT,
uEXP2_MAX_INPUT,
uEXP2_MIN_THRESHOLD,
uHALF_UNIT,
uLOG2_10,
uLOG2_E,
Expand Down Expand Up @@ -172,6 +173,9 @@ function exp(SD59x18 x) pure returns (SD59x18 result) {
if (xInt > uEXP_MAX_INPUT) {
revert Errors.PRBMath_SD59x18_Exp_InputTooBig(x);
}
if (xInt < uEXP2_MIN_THRESHOLD) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexplained. We need to add an explanatory comment

return ZERO;
}

unchecked {
// Inline the fixed-point multiplication to save gas.
Expand Down Expand Up @@ -202,7 +206,7 @@ function exp2(SD59x18 x) pure returns (SD59x18 result) {
int256 xInt = x.unwrap();
if (xInt < 0) {
// The inverse of any number less than this is truncated to zero.
if (xInt < -59_794705707972522261) {
if (xInt < uEXP2_MIN_THRESHOLD) {
return ZERO;
}

Expand Down
10 changes: 5 additions & 5 deletions test/unit/sd59x18/math/exp2/exp2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
pragma solidity >=0.8.19 <0.9.0;

import { sd } from "src/sd59x18/Casting.sol";
import { E, EXP2_MAX_INPUT, MIN_SD59x18, MIN_WHOLE_SD59x18, PI, UNIT, ZERO } from "src/sd59x18/Constants.sol";
import { E, EXP2_MAX_INPUT, MIN_SD59x18, MIN_WHOLE_SD59x18, PI, UNIT, ZERO, uEXP2_MIN_THRESHOLD, EXP2_MIN_THRESHOLD } from "src/sd59x18/Constants.sol";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has to be ordered alphabetically

import { PRBMath_SD59x18_Exp2_InputTooBig } from "src/sd59x18/Errors.sol";
import { exp2 } from "src/sd59x18/Math.sol";
import { SD59x18 } from "src/sd59x18/ValueType.sol";

import { SD59x18_Unit_Test } from "../../SD59x18.t.sol";

contract Exp2_Unit_Test is SD59x18_Unit_Test {
/// @dev Any input smaller than this makes the result zero.
SD59x18 internal constant THRESHOLD = SD59x18.wrap(-59_794705707972522261);

function test_Exp2_Zero() external pure {
SD59x18 x = ZERO;
Expand All @@ -28,7 +26,7 @@ contract Exp2_Unit_Test is SD59x18_Unit_Test {
delete sets;
sets.push(set({ x: MIN_SD59x18, expected: 0 }));
sets.push(set({ x: MIN_WHOLE_SD59x18, expected: 0 }));
sets.push(set({ x: THRESHOLD - sd(1), expected: 0 }));
sets.push(set({ x: EXP2_MIN_THRESHOLD - sd(1), expected: 0 }));
return sets;
}

Expand All @@ -39,7 +37,9 @@ contract Exp2_Unit_Test is SD59x18_Unit_Test {

function negativeAndGteMinPermitted_Sets() internal returns (Set[] memory) {
delete sets;
sets.push(set({ x: THRESHOLD, expected: 0.000000000000000001e18 }));
sets.push(set({ x: EXP2_MIN_THRESHOLD, expected: 0.000000000000000001e18 }));
sets.push(set({ x: EXP2_MIN_THRESHOLD - sd(1), expected: 0 }));
sets.push(set({ x: -sd(2**255-1), expected: 0 }));
Comment on lines +40 to +42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smallest value has to be at the top

Comment on lines +40 to +42
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use MIN_SD59x18 instead of -sd(2**255-1).

sets.push(set({ x: -33.333333e18, expected: 0.000000000092398923e18 }));
sets.push(set({ x: -20.82e18, expected: 0.000000540201132438e18 }));
sets.push(set({ x: -16e18, expected: 0.0000152587890625e18 }));
Expand Down
Loading