0

I have an issue creating a calculation based smart contract which performs some calculation based on some formula. Here is the function as bellows:

 function financialReputation( ufixed xb , ufixed xs) returns (uint vb , ufixed vs) {
//uint128 exp = 2.718;
vb = - (1 / 2**(uint(xb / 5) )) +1;
vs = - (1 / ufixed(2.7182818285)**(ufixed(xs / 2000)) ) +1;
// xi = (1 / (1+ 2**(- 0.005 * xi - 4) ) )** 50;
return (vb,vs);
}

Error will be as :

TypeError: Operator ** not compatible with types ufixed128x18 and 
ufixed128x18
vs = - (1 / ufixed(2.7182818285)**(ufixed(xs / 2000)) ) +1;
        ^---------------------------------------^

Please help me how i can convert ufixed to a compatibal type for exponent operator(**). Any suggestion or recommendation will be requested.

0

You probably need to use some kind of fixed point math library such as ABDK Math 64.64. It operates with binary fixed point numbers (64 binary digits after dot) and has all basic math operations including logarithm and exponent. This should be enough to implement your formula.

1

Solidity only allows you to use the exponent operator with integer values on the right side of the operation. You can solve your error by casting the ufixed(xs / 2000) to a uint256 by writing uint256(ufixed(xs / 2000)). Keep in mind that this will probably result in unintended side effects in the future as casting usually truncates the higher-order bits and fixed-point number types use a special bit layout for representing the said numbers.

Regardless, even if you implement the casting mentioned above, fixed point numbers have not yet been implemented in Solidity so your contract will (for now) never compile.

  • Thanks so it means i have to use integers but how i can use float numbers as in solidity documents they mentioned about the ufixed and fixed for rational(decimal numbers). Without this my contract is useless because i need to calculate as per formula and need to distribute correct numbers with decimal points. Please suggest me, really appreciated. – Aman Kamboj May 24 '18 at 10:10
  • @AmanKamboj As I mentioned, Solidity does not support decimal point numbers. You are out of luck so you can either wait for a release of Solidity that supports them or create them yourself from scratch which I highly discourage. – Alex Papageorgiou May 24 '18 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.