Fixed Point Arithmetic : how Solidity handles it


1 min read

Solidity uses fixed point arithmatic instead of floating point arithmatic to reduce computational cost (gas) significantly.

In solidity the fixed point arithmatic representation is expressed in decimal which is base 10.

But first lets take an example of how to convert a fractions in fixed point . For example convert fractional number like 2.38 to fixed point arithmatic decimal,

  1. Multiply the fractional number by a power of 10 to shift the decimal point to the right by the desired number of decimal places. lets say we want two decimal places, we multiply by 100.

2.38 * 100 = 238

  1. Round the result to the nearest integer if necessary. In this case, 238 is already an integer.

  2. Write the number as the integer value with the appropriate scaling factor. For two decimal places, the scaling factor is 100.

238 * 100 = 23800

So, 2.38 can be represented as 23800 in fixed-point arithmetic decimal with two decimal places.

In the above example we 2 as scaling factor similiar process applies to sacling factor such as 18 that we see for erc20 tokens.

Fixed-point arithmetic is a way to represent and perform arithmetic operations on fractional numbers using a fixed number of decimal places. The scaling factor determines the precision or the number of decimal places in the representation.