Solidity Compiler – Is Floating Point Number Rounding Occurring During Compilation?

floating-pointmathsolidity

I understand you cant represent them while writing the code, but are floating-point numbers rounded during the compilation?

Imagine there is an equation in code, that needs some sort of floating-point number, I.e 0.1. Since we can't write this directly, can we divide 10 on 100 to produce 0.1? Will compiler round this down to 0?

P.S I'm well aware of libraries mentioned here https://ethereum.stackexchange.com/a/83786/64937, but in my case, I don't want that much flexibility and complexity, but eventually, if there is no way I will use them.

Best Answer

The compiler does accept decimals in an expression if the resulting value is valid.

For example uint256 x = 0.23 * 100 is valid. The expression uint256 y = 0.123 * 100 fails to compile with

TypeError: Type rational_const 123 / 10 is not implicitly convertible to expected type uint256.

Related Topic