Giter Site home page Giter Site logo

💥 Math Functions about snekmate HOT 3 CLOSED

bout3fiddy avatar bout3fiddy commented on June 11, 2024
💥 Math Functions

from snekmate.

Comments (3)

pcaversaccio avatar pcaversaccio commented on June 11, 2024

Examples: https://github.com/bout3fiddy/tricrypto-ng/blob/ab37f895cae1a5ef2c95b6c7626c47fe8b180b7b/contracts/CurveCryptoMathOptimized3.vy

from snekmate.

pcaversaccio avatar pcaversaccio commented on June 11, 2024

some functions I want to implement:

  • uint256_average
  • int256_average
  • ceil_div
  • log_2
  • log_10
  • log_256

from snekmate.

pcaversaccio avatar pcaversaccio commented on June 11, 2024

For the sake of history, below is a correct but inefficient (costs around 16,801 gas) implementation for cbrt (won't be part of 🐍 snekmate therefore :)):

# @version ^0.3.7


@external
@pure
def cbrt(x: uint256) -> uint256:
    y: uint256 = empty(uint256)
    z: uint256 = empty(uint256)
    value: uint256 = x

    i: uint256 = shift(1, max_value(uint8))
    for _ in range(86):
        y = shift(y, 1)
        z = unsafe_add(unsafe_mul(unsafe_mul(3, y), unsafe_add(y, 1)), 1)
        if (unsafe_div(value, i) >= z):
            value = unsafe_sub(value, unsafe_mul(i, z))
            y = unsafe_add(y, 1)

        i = shift(i, -3)

        if (i == empty(uint256)):
            break

    return y

PS: For 2**256-1 it costs 18,851 gas:

image

from snekmate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.