5

Where can I find the maximum and minimum values that various types can store? And an epsilon value for floats?

I'm looking for something like C's limits.h, but for Solidity / EVM.

13

I assume you mean the integer types, because those are really the only types in Solidity that have a maximum and a minimum.

Solidity does not support floating point types, and most likely will never because they are considered not to be precise enough. Ethereum contracts need to be 100% deterministic, and always run the same way on all hardware.

Solidity will in the future support fixed-point types, but it doesn't yet.

Firstly, int means int256 and uint means uint256. Once you know the amount of bits your integer has, you can easily calculate the minimum and maximum using bitwise arithmetic:

int256 constant INT256_MIN = int256(uint256(1) << 255);
int256 constant INT256_MAX = int256(~(uint256(1) << 255));
uint256 constant UINT256_MIN = 0;
uint256 constant UINT256_MAX = ~uint256(0);

All the bitwise shifting is done on unsigned ints to avoid any special behaviour on the sign bit.

  • Cheers! FYI, standard like IEEE754 would run deterministically on all hardware. Regardless, there is a virtual machine abstraction layer. – Tom Hale Oct 5 '17 at 8:35
  • @TomHale Yeah, a standard is a standard ofcourse. Thing is, I've had problems with 64-bit float arithmetic being compiled to extended precision 80-bit float arithmetic without anyone noticing. After the calculations were done, the compiler converted the 80-bit float back to 64-bit float. This caused apparent differences on different hardware, which was kind of a disaster. Ofcourse it was the compiler's and the programmers' fault, not the hardware's or the standard's. I guess I'm a bit prejudiced against floats :-) – Jesse Busman Oct 5 '17 at 8:46

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.