What is fixed-point and floating-point?

August 10, 2024

Background

Did you heard DEX service 'Uniswap'? This service is famous and used broadally. I also have known that service and even used this service for swap token by token.

I want to know the mechanism using in Uniswap, so read the whitepaper. Uniswap has AMM(Automated Market Maker) & CPMM(Constant Product Market Maker). It seems to be same at result but different conceptually. But this article is not for that so just skip it.

In V2 or V3, I see specializied data type, Q64.96. This type is associated with Fixed Point. It is a concept of Computer Science for handling real number. Commonly computer should convert the real number to binanry with 32 bits.

Floating Point

Before writing about fixed point, I write about Floating Point. It is literally that a method expressing real number with the point floating.

Floating point consists of three point, Sign, Exponent and Mantissa, Sign is expressing that a number is possitive(0) or negative(1). Mantissa is serialized bits converted from real number without decimal points. Exponent is the amount of squre calculation. Mantissa is serialized bits converted from real number without decimal points. So, Mantissa is shifted to left by amount of exponent, and decide sign, finally that can be a real number.

IEEE-754 is noticed that sign has 1 bit, exponent has 8bit, and mantisa has 23bit.

Floating Point Sample

For example, there are a number, 17.5. you should normalize that number. mantisa will be 1.00011. Exponent will be 10000100, because for making the binary number 1.0011 to 17.5 in decimal, you should multifly 2^4. Actually the head of exponent 1 is bias.

Floating point is difficult to calculate real number to binary. But that can express the larger scale of numbers than fixed point beacause they move the point's location they need.

Fixed Point

Fixed point is so easy. Just divide section expressing the real number with two parts, interger part and fractional part. And they have sign bit. In 32 bits systems, interger part made of 15 bits and fractional part made of 16 bits.

Fixed Point Sample

For example, there are a number, 16.625. Interger part will be 10000. Fractional Part will be 101. Interger part align right side, and fractional part align left side in each part.

Fixed point is simple to express the real number. but beacuse they fix the location of decimal point, fixed point has smaller scale of expressing the real number.

What is fixed-point and floating-point?