Tezos Asked by George Mathew Kanianthara on August 20, 2021
If not,is there any alternative way to implement a natural log component into your smart contract code?
As explained by Arvidj, there are no floating point numbers in Michelson (or SmartPy), no exp
or log
function.
While working with natural numbers, you can implement by hand some examples which may or may not be enough for you. Some examples here: https://smartpy.io/dev/index.html?template=calculator.py, https://smartpy.io/dev/index.html?template=worldCalculator.py
A complete example for you:
import smartpy as sp
class Calculator(sp.Contract):
def __init__(self):
self.init(value = 0)
@sp.entry_point
def test(self, x):
self.data.value = self.log2(x)
@sp.global_lambda
def log2(x):
result = sp.local('result', 0)
y = sp.local('y', x)
sp.while 1 < y.value:
result.value += 1
y.value //= 2
sp.result(result.value)
if "templates" not in __name__:
@sp.add_test(name = "Calculator")
def test():
c1 = Calculator()
scenario = sp.test_scenario()
scenario += c1
scenario += c1.test(1000)
scenario.verify(c1.data.value == 9)
EDIT. Adding a fixed precision implementation in SmartPy.
https://smartpy.io/dev/index.html?template=fixed_precision.py
Correct answer by FFF on August 20, 2021
I have a contract for binary log that uses few multiplications and can be adjusted for any fixed point precision: https://github.com/Sophia-Gold/michelson/blob/master/log2fix.tz. I don't think it can be written in SmartPy because last I checked it lacks shifts. Depending on your needs you could use this with change of base or Arthur's method.
Answered by Sophia Gold on August 20, 2021
You have to do it manually... Here's one approach for compute log(x)
assuming you are representing x
as a fraction
Answered by Arthur B on August 20, 2021
There is no such instruction in Michelson, so probably the same goes for SmartPy. Furthermore, Michelson does not having floating point values. You could implement some version of natural log yourself using repeated divisions, however, precision will suffer. Also, note that this can be costly in gas.
One option is to have the contract take the contract take in parameter the exponent calculated off-chain, and then have the contract verify that the exponent is indeed correct. Instead of (in pseudocode):
def contract(some_parameter):
exponent = nl(some_parameter)
have
def contract(expected_exponent, some_parameter):
assert e^expected_exponent = some_parameter
but again, you will turn into the issue that exponentiation is not available in Michelson (but which you could implement with repeated multiplications), and nor are floating points values.
We can perhaps help you better if you give more context to the issue you want to resolve?
Answered by arvidj on August 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP