-
Notifications
You must be signed in to change notification settings - Fork 56
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
implement log2
, log10
, log256
(and possibly ln
)
#123
Comments
+1. Also been looking for this functionality for some time. I'm unsure of the best method to implement this? |
there are a few different solidity libraries that implement logarithm. off the top of my head, the big name i know is oz, they have log2,log10,log256 in their math libraries: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/Math.sol it may be desirable to replicate the results (not method, we can probably do better) of one such library (not neccesarily oz's), since I think mirroring or performing on-chain operations is a common use case for this package. |
+1 yes, that was my reasoning for requesting this, because I wanted to match some of the LOG math used in smart contracts |
happy to do some work on this. personally we use log2 which LOG math functions do you have immediate needs for? i can start with those and add more down the line log2? log10? log256? |
Log2 is enough for UniswapV3 tickmath: |
Is there any difference between |
log2
, log10
, log256
(and possibly ln
)
Yes, |
FWIW, here's a set of log implementation for big.Int https://github.com/robpike/ivy/blob/master/value/log.go |
Possible api-methods could be
However, it would be simpler to just do
Then the caller can just do Also, in fact, the |
https://gfx.cafe/-/snippets/10#LC16 the log2 required in uniswap is just msb, so bitlen - 1 at the very least it's correct when going from tick -> sqrt price -> tick iirc msb is always bitlen - 1. if thats the case i dont see a use in adding a dedicated msb function |
Yeah implementing |
uniswap does |
Adds the following methods ```golang // CmpUint64 compares z and x and returns: // // -1 if z < x // 0 if z == x // +1 if z > x func (z *Int) CmpUint64(n uint64) int // Log10 returns the log in base 10, floored to nearest integer. // **OBS** This method returns '0' for '0', not `-Inf`. func (z *Int) Log10() uint ``` The implementation of `Log10` is probably close to the optimal way. The majority of cases are handled by one lookup based on the bitlength. For the remaining cases, one an additional lookup is performed, a comparison, and then it is done. This PR adds around 2K persistent memory for lookup tables and precalculated integers. ``` lut [257] uint8 // 257 B pows = [60]Int // 1920 B pows64 = [20]uint64 // 20*8 = 160 B ``` Ref: #123
Can there be a natural log or base 2 log implemented?
Thanks
The text was updated successfully, but these errors were encountered: