Search code examples
ethereumsolidity

Exponentiation of fixed-point numbers (i.e., a^b) in Solidity


I already know how to calculate x^n, where x is a floating point number and n is an integer. However, I want to implement a price curve, using the formula:

y = 0.5 * (x^1.5)

To do this, I need to be able to do exponentiation where the power is a floating-point (or fixed-point) value. How can I do this in Solidity?


Solution

  • Solidity does not offer built-in math functions, therefore you must use a library.

    Paul Berg's PRB-math library provides fixed-point mathematics operations for Solidity.