# Tag Info

17

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 ...

16

Problem is 4 & 5 fit in less that 256 bits (each). You end up with tiny uints in the constant expression, and then those aren't easily converted to the uint256, so ... cast the type explicitly. uint x = uint(4)/uint(5); Takeaway is caution with constants because they may be cast in unexpected types. A sketchy idea: contract Divide { function ...

12

With regard to Solidity... This is really more a general computer science question that would best be answered on Stack Overflow. At the risk of repeating what @Ismael has linked to... U - unsigned (meaning this type can only represent positive integers, not positive and negative integers) INT - integer 256 - 256 bits in size Context: The EVM (Ethereum ...

5

Here's a simple example to demonstrate casting msg.value to a uint248 value using Browser Solidity with the following code: pragma solidity ^0.4.8; contract Test { uint248 public value; function Test() { value = 123; } function () payable { value = uint248(msg.value); } } The screen below shows the deployment of the ...

5

Integers in Solidity: uint256 (uint is an alias) is a unsigned integer which has: minimum value of 0 maximum value of 2^256-1 = 115792089237316195423570985008687907853269984665640564039457584007913129639935 //78 decimal digits int256 (int is an alias) is a signed integer which has: minimum value of -2^255 = -...

4

For casting, use value = uint248(msg.value); Casting to save 8 bits in this case is not worth it and will probably cost more gas due to unpacking: see Why does uint8 cost more gas than uint256? Even when using a struct of uint248 and uint8, it is best to actually test whether you are getting some gas savings.

4

Try this: value = uint248(msg.value);

4

Others have pointed in the right direction, but let me try to specifically answer the questions. First, each 256-bit word of contract storage is very expensive. When a contract is using smaller variables, such as uint32 (32 bits), then Solidity will try to pack multiple variables into one storage word. Most of what you see here is the compiler first ...

4

web3.toBigNumber("20000000000000000000").minus(web3.toBigNumber("19999999999999999989"))

4

Update as of 2020. Solidity 0.6.8 introduced min and max keywords that can now natively tell you the min and max of an expected type. From the release page: Implemented type(T).min and type(T).max for every integer type T that returns the smallest and largest value representable by the type. You can try it out with the following code. Note that the ...

3

as an addition to @rob's answer you can use : function calcul(uint a, uint b, uint precision) view returns ( uint) { return a*(10**precision)/b; } If we divide using the function above 7/3 with a precision of 5 it will output 233333 which means 7/3=2.33333. The conversion to float can be done in the front-end.

2

Just thinking about this a bit more... Your code takes a (signed) int256 as input. The maximum (absolute) value of this is around 1e+76, which would equate to 76 cycles of the while loop, each composed of multiple instructions. In such cases it would probably be cheaper to use a logarithmic method - i.e. take the log10 of the input and ignore any ...

2

Okay so I worked this out. The u in uint is 'unsigned' (duh) so when javascript sends in -1 Solidity sees it as 1.157920892373162e+77. In my actual code I had an additional modifier that checked that the amount deposited was actually approved (depositing ERC20 tokens) and that's why the test was working. Nothing like coding solidity to make one feel like ...

2

If you're only interested in those three comparison operators, then it's pretty easy: struct uint512 { uint256 hi; uint256 lo; } function eq(uint512 x, uint512 y) internal pure returns (bool) { return x.hi == y.hi && x.lo == y.lo; } function lt(uint512 x, uint512 y) internal pure returns (bool) { return x.hi < y.hi || (x.hi == y....

2

It's a bit funny that the Solidity documentation hints about such a function abs (https://solidity.readthedocs.io/en/v0.5.3/types.html) but I can't get it to compile at least in Remix. So I assume there is no such function and the documentation is just a bit off (wouldn't be the first time). There are probably some libraries which contain that functionality ...

1

So, I found a convincing verification via this link. In python there exists a function that turns a hex string into a byte representation like so: >>> preimage = bytes.fromhex('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...

1

If you remove the {} then the for loop will only execute the next statement. i.e a=b. Then when it completes, the return statement is executed hence you have value of 19.

1

The link that Shawn Tabrizi posted may provide the reason why Solidity itself doesn't throw errors for integer overflow. However, there are helpful developer tools that can help mitigate these errors. When deploying a contract I'll usually upload my abi to Amberdata.io which runs automated security audits. For instance, with the 0x Protocol contract it shows ...

1

The error message says: [number-to-bn] while converting number  to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "3600" This means that instead of  you should pass either one of the following: 3600 "0xE10" web3.utils.toBN(3600) new ...

1

You entered an array of one number instead of the number itself. Change arguments: [ "0x84f0c8fC2F6bc8394EB77BaAAe89cB6e12C048C2", [86400/*here is duration*/] ] to arguments: [ "0x84f0c8fC2F6bc8394EB77BaAAe89cB6e12C048C2", 86400] and it should work

1

If you're not worried about the exactly 0% and exactly 100% cases (or alternatively, the num == 0 and num == denom cases), then yes, num > 0 && num < denom is correct (with an implied denom > 1, anyway). The proof is fairly simple: 0 < num / denom < 1 can be rewritten as 0 * denom < num < 1 * denom, which reduces to 0 < num &...

1

You may use fixed point math library such as ABDK Math 64.64. It has method divi that divides one integer by another and returns the result as binary fixed point number with 64 binary digits after dot.

1

Literal expressions have arbitrary precision until converted (eg. casted) to a non-literal type. In this example, the literal expression 5/4 is an internal rational constant type with unlimited precision, and cannot be implicitly downcasted to a uint256. This is apparently a restriction on rationals imposed by the compiler. Note that for whole-number ...

1

This code returns -1 for me (for both int8 and int): pragma solidity ^0.4.6; contract NumTest { int8 i; function NumTest() { i = -1; } function number() constant returns (int8 num) { return i; } } As has been mentioned in previous threads, due to unpacking costs, the int8 case (278 gas) is actually more expensive ...

1

Your contract code works fine: your variable count is public anyways. you can just try using the automatic getter SimpleTestInstance.count() my guess the problem you had was with uint and int. Remember that int or uint stands for uint256 and int256. So depending on the type signatures of your contracts, it may be wrong. link to fiddle: https://ethfiddle....

1

It sounds like a compiler error (I have no idea what is "formal verification section"). Most likely Solidity compiler has been upgraded to a newer version that comes with stricter checks. However it is impossible to tell unless you include your contract source code and actual errors in the question. Please edit the question and use Markdown code ...

1

function magnitude (uint x) public pure returns (uint) { require (x > 0); uint a = 0; uint b = 77; while (b > a) { uint m = a + b + 1 >> 1; if (x >= pow10 (m)) a = m; else b = m - 1; } return a; } function pow10 (uint x) private pure returns (uint) { uint result = 1; uint y = 10; while (x > 0) { ...

1

Here's how I did it. There is probably significant scope for improvement. I would accept any answer that uses less gas: pragma solidity ^0.4.6; contract Magger { function getOrderMag(int256 input) constant returns (int256){ int counter=0; if (input<0){ input=input*-1; } while((input/10)>=1){ ...

Only top voted, non community-wiki answers of a minimum length are eligible