prb-math icon indicating copy to clipboard operation
prb-math copied to clipboard

Allow negative inputs in the `pow` function

Open xenoliss opened this issue 3 years ago • 4 comments

Hi there,

I'm trying to use the pow function of the PRBMathSD59x18 library but with a negative x as input. Would be interesting if that could be implemented as powering a negative integer totally makes sense but is currently forbidden because of the usage of log2 on x.

Thanks !

xenoliss avatar Oct 01 '21 09:10 xenoliss

Hey! thanks for sharing your feedback.

Supporting negative bases for the pow function is difficult .. the only solution I can think of is to calculate the signs and the absolute values separately. But that sounds expensive in gas terms.

I'll see what I can do about this. In the meantime, could you share what is the use case? What do you need negative bases for?

PaulRBerg avatar Oct 02 '21 12:10 PaulRBerg

Thanks for the response.

Basically I'm implementing a function that takes two int256 and takes the difference of them and raise the result to a given power. I can't assume that the difference will always be positive (but fortunately the power will always be even so it's okay in my specific case). So in my specific case I'm doing something like diff.abs().pow(2*10**18) and it's okay, but I found it not super friendly to not be able to give the pow function a negative integer (due to the log2).

That being said I understand that it might lead to more gas consumption but I'm sure there are usecases where this is just the way to go.

xenoliss avatar Oct 02 '21 16:10 xenoliss

Maybe it should take an unsigned integer for X, instead?

We have been breaking our heads here as to why -3**3 was returning 27 instead of -27.

Also, good job with the lib, thanks for publishing it! We have been breaking our heads as to why some functions would return numbers after dividing them by 1018, while others don't? Same thing for the parameters of these functions, some are taking the numbers *1018, for example (3000000000000000000,3), while others take the plain numbers, like (3,3) So we were wondering if it's us that are missing something crucial or if it's lack of documentation or clarity on the examples on how to use the lib?

PinkFromTheFuture avatar Jan 16 '23 19:01 PinkFromTheFuture

Hey @PinkFromTheFuture, thanks for the feedback and I'm sorry you had a negative experience with some of the functions of PRBMath. I invested a lot of effort into documenting each and every function with thoughtful NatSpec comments, plus code comments within the function body. I encourage you to take a look at the source code - all of the function parameters are annotated:

https://github.com/PaulRBerg/prb-math/blob/642a4c948daf51266564a445c9bfe7f4a228eae0/src/sd59x18/Math.sol

Regarding your comment about "plain numbers" - it's really only the powu (power unsigned) function that uses a basic number as an exponent.

I don't have a dedicated documentation website for PRBMath at the moment, though I might create one if I keep getting requests for one.

In the meantime, if you have any specific questions regarding how to use PRBMath, feel free to post them in the Discussions channel. I will try to answer as soon as I can.

PaulRBerg avatar Jan 16 '23 20:01 PaulRBerg