Fixed point

From DSWiki

Jump to: navigation, search

[edit] introduction to fixed point numbers

The DS has no float point unit, but it supports fixed point math. What are fixed point numbers? They are integers with some digits interpreted as the fraction, for example we could interprete 1001 as 10.01 (last two digits are the fraction). We can add two fixed point numbers the same way, as we would add integers:

 fixed point:
  10.01
 +13.99
 =24.00
 
 integer:
  1001
 +1399
 =2400

This makes fixed point easier to implement than float point, because we already have an integer ALU in our CPU. A multiplication of two fixed point numbers is a bit more complicated, but not so much:

 12.13 * 2.05 = 24.86 (we only have 2 digits fraction)
 as an integer, we get:
 1213 * 205 = 248665

So, we can use integer multiplication, but be have to shift the result 2 digits to the right. Because a computer works with a binary system, we have to work with n binary digits for the interger part of the number and m binary digits for the fraction, in most cases we want negative numbers too, so we need one bit sign.If we say, we have a 1.15.16 fixed point number, this means, we have one bit sign, 15 bits for the integer part and 16 bit fraction. This number would fit into a 32 bit signed integer.

[edit] what we have on the DS

For the DS we need this fixed point numbers:

  • v16 = 1.3.12 fixed point number (used for 3D)
  • t16 = 1.11.4 fixed point number
  • f32 = 1.19.12 fixed point number (used for matrices)
  • v10 = 1.0.9 fixed point number (whoops! 10 bits don't fit into a normal integer? but 3 v10 numbers fit into a 32 bit integer, so this format is used for normals in 3D (it's also ok for normals to be between -1 and 1, so this is why these fixed point numbers have a long fraction but no integer part!)
  • 0.8.8 fixed point number: this format is used for the scaling of the extended rotation backgrounds ans doesn't have a typedef in the NDSlib.