Fixed point vs Floating point number

前端 未结 5 1754
故里飘歌
故里飘歌 2020-12-12 09:46

I just can\'t understand fixed point and floating point numbers due to hard to read definitions about them all over Google. But none that I have read provide a simple enough

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-12 10:27

    From my understanding, fixed-point arithmetic is done using integers. where the decimal part is stored in a fixed amount of bits, or the number is multiplied by how many digits of decimal precision is needed.

    For example, If the number 12.34 needs to be stored and we only need two digits of precision after the decimal point, the number is multiplied by 100 to get 1234. When performing math on this number, we'd use this rule set. Adding 5620 or 56.20 to this number would yield 6854 in data or 68.54.

    If we want to calculate the decimal part of a fixed-point number, we use the modulo (%) operand.

    12.34 (pseudocode):

    v1 = 1234 / 100 // get the whole number
    v2 = 1234 % 100 // get the decimal number (100ths of a whole).
    print v1 + "." + v2 // "12.34"
    

    Floating point numbers are a completely different story in programming. The current standard for floating point numbers use something like 23 bits for the data of the number, 8 bits for the exponent, and 1 but for sign. See this Wikipedia link for more information on this.

提交回复
热议问题