Nat subtraction overflow problem

Nat subtraction overflow problem
0-1 in motoko will report an error Natural subtraction underflow
But in solidity 0-1 will return 115792089237316195423570985008687907853269984665640564039457584007913129639935
This will cause a lot of procedural problems

I know because in solidity, the maximum is uint256 int256, so you can use the maximum value to subtract,
In motoko, nat int has no upper limit precision, so there is no way to deal with it, but is there really a scene with unbounded precision? Can motoko nat int have a maximum accuracy of 64? Can it be used in all scenarios?

1 Like

If you really want wrap-around arithmetic, you can use Nat64 with the -% operator. But are you sure you want that?

Yes, I need it, but the precision of nat64 is not enough for my data calculation.

Then use Int and wrap your operations in … % 2**256 maybe?

Has motoko considered giving more nat formats based on more scenarios? nat128-256 Int 128-256 format to make writing programs more convenient?

There has been discussion. Back then the reasoning for not providing more was “where do we stop” and then “lets just stick what’s natively provided my WebAssembly”. But then we were explicitly not building a blockchain back then, but a decentralized cloud, so :man_shrugging:

1 Like