Macro returns unexpected floating point number
Closed this issue · 1 comments
itirabasso commented
Hi! I'm working on a buidler plugin for solpp to integrate it on the compilation pipeline, but I'm having a few problems with some macros that I found out on the readme's example.
I have this contract A.sol
pragma solidity >=0.4.21 <0.6.0;
contract A {
// #def POW(a, b) a ** b
constructor() public {
uint n1 = $(POW(2, 3)) + $$(POW(16, 0.5));
}
}
and then I run
npm init
npm install solpp@^0.9.1
npx solpp A.sol
this retrieves the following output:
pragma solidity >=0.4.21<0.6.0;
contract A {
constructor() public {
uint n1 = 2 ** 3 + 3.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
}
}
which makes the compilation fail.
merklejerk commented
Exponentiation is always done in floating point, so there is no guarantee you'll get back an integer with a perfect root.
I would recommend using round()
to ensure you always end up with an integer, if that's what's needed. Something like:
contract A {
// #def POW(a, b) a ** b
constructor() public {
uint n1 = $(POW(2, 3)) + $$(round(POW(16, 0.5)));
}
}