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

Allow negative inputs in the pow function #52

Open
xenoliss opened this issue Oct 1, 2021 · 4 comments
Open

Allow negative inputs in the pow function #52

xenoliss opened this issue Oct 1, 2021 · 4 comments
Labels
effort: epic Multi-stage task that may require multiple PRs. type: feature New feature or request. work: complicated Sense-analyze-respond. The relationship between cause and effect requires analysis or expertise.

Comments

@xenoliss
Copy link

xenoliss commented Oct 1, 2021

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 !

@PaulRBerg
Copy link
Owner

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?

@xenoliss
Copy link
Author

xenoliss commented Oct 2, 2021

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.

@PaulRBerg PaulRBerg changed the title Allow for negative x parameter for the pow function [prb-math] Allow for negative x parameter for the pow function Oct 5, 2021
@PaulRBerg PaulRBerg changed the title [prb-math] Allow for negative x parameter for the pow function Allow for negative x parameter for the pow function Oct 20, 2021
@PinkFromTheFuture
Copy link

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 *10
18, 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?

@PaulRBerg
Copy link
Owner

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 PaulRBerg changed the title Allow for negative x parameter for the pow function Allow negative inputs in the pow function Apr 13, 2023
@PaulRBerg PaulRBerg added effort: epic Multi-stage task that may require multiple PRs. type: feature New feature or request. work: complicated Sense-analyze-respond. The relationship between cause and effect requires analysis or expertise. and removed priority2 labels Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: epic Multi-stage task that may require multiple PRs. type: feature New feature or request. work: complicated Sense-analyze-respond. The relationship between cause and effect requires analysis or expertise.
Projects
None yet
Development

No branches or pull requests

3 participants