Solidity: Division of two constant numbers gives error when stored in uint256

Created on 14 Nov 2017  路  6Comments  路  Source: ethereum/solidity

Solidity:
version=soljson-v0.4.18+commit.9cf6e910.js
version=soljson-v0.4.17+commit.bdeb9e52.js

Source

The code responsible for error is:

uint256 public result = (50000e18*2500)/300;

Problem

The error received is:

Error: Type rational_const 1250000000000000000000000/3 is not implicitly convertible to expected type uint256. Try converting to type ufixed256x53 or use an explicit conversion.

An explicit conversion to uint256 tells Not yet implemented - FixedPointType.

Workaround

Currently, all constants are considered of ufixed datatype. To enable division of constants, one of them must be uint256. One way to do that is as follows:

uint256 val = 3/uint256(2)

Most helpful comment

Yes! Till then I can use a workaround by making one of the numbers as uint256 and then perform the division.

uint256 public result = 1250000000000000000000000/uint256(3);

All 6 comments

Numbers actually have an internal type of "RationalNumber" which is attempted to be converted into either an integer (<= 256 bits) or a fixed point type. Your example number can only be converted into a fixed point type as it is not an integer and since fixed point types are not fully supported yet it will fail.

Can you please explain your suggestion? What I understand is you would want the compiler to do truncated division without any warning?

Thanks for this useful information. This issue was not a suggestion, but more like a question. I expected those numbers to be able to be int and be able to be converted to uint256 implicitly. However, I still have a doubt:

Your example number can only be converted into a fixed point type as it is not an integer

Is 50000e18*2500 not a integer? I believed integers can range till 2**256

Another question:

What will be the result of int/int?

The expression (50000e18*2500)/300 is evaluated and considered as a single number, which is 1250000000000000000000000/3.

See http://solidity.readthedocs.io/en/develop/types.html#rational-and-integer-literals

What data type should be used to store the result? I was unable to find a datatype to store the result. Following data types did not work:

fixed public result1 = 1250000000000000000000000/3; //same as previous error
ufixed128x19 public result2 = 1250000000000000000000000/3; //same as previous error
ufixed256x53 public result = 1250000000000000000000000/3; //UnimplementedFeatureError: Not yet implemented - FixedPointType.

The support for fixed point types is incomplete. You will not be able to store this result in any type currently.

If you are certain you need this number, consider doing fixed point types by hand, but it is very likely that you could avoid it by restructuring your code.

Yes! Till then I can use a workaround by making one of the numbers as uint256 and then perform the division.

uint256 public result = 1250000000000000000000000/uint256(3);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hiqua picture hiqua  路  4Comments

axic picture axic  路  3Comments

leviadam picture leviadam  路  4Comments

chriseth picture chriseth  路  3Comments

walter-weinmann picture walter-weinmann  路  4Comments