solidity exp() differs from javascript sdk exp() #109
-
With the same input, the Is this a bug? Or am I doing something wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Hey! Thanks for opening this discussion. This is not a bug. If you were to use TypeScript instead of JavaScript, you would have seen that you cannot pass JavaScript numbers directly. You can only pass Ethers BigNumbers to the PRBMath functions: JavaScript numbers are notoriously unreliable. Instead, what I recommend doing is to use my library evm-bn to convert stringified values to Ethers BigNumbers: import { toBn } from "evm-bn";
log(exp(toBn("3174150177198", 18)).toString()); Note: I am not sure why you wanted to raise to power "3174150177198". Raising e to that power gives you a stupendously large number, which doesn't fit in |
Beta Was this translation helpful? Give feedback.
-
Assuming that you used It is not possible (or just very difficult) to have exact parity between mathematical functions implemented in different programming languages. And in our case, we are confronted with two very different languages: Solidity and JavaScript. If you're writing Hardhat tests, you shouldn't check for exact equality between outputs. Instead, you should use an assertion such as Waffle's It might be instructive to take a look at the tests I have written for And: |
Beta Was this translation helpful? Give feedback.
Assuming that you used
evm-bn
, and the intention was to raise to power0.000003174150177198
(3174150177198
in Solidity), then the behavior you're observing is completely expected.It is not possible (or just very difficult) to have exact parity between mathematical functions implemented in different programming languages. And in our case, we are confronted with two very different languages: Solidity and JavaScript.
If you're writing Hardhat tests, you shouldn't check for exact equality between outputs. Instead, you should use an assertion such as Waffle's
closeTo
, or the one I wrote for PRBMath itself, thenear
assertion.It might be instructive to take a look at the tests I have written …