#### Archived

This topic is now archived and is closed to further replies.

# Fixed Point Math, how do you use it? and why?

This topic is 5371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

quote:
Original post by Vanukoff For those of us who are apprehensive about using floating points in our code (yeah yeah yeah, I know modern FPU/CPU are fast) anyway I am using a fixed point solution (IMO fixed point is more suitable to 2D game, I'd use floating point in a 3D game however). We all know 1000 milliseconds = 1 second. 1000 is almost 1024, which is 2^10. I smell an optimization. Pseudocode:


game_play(int deltatime)  // game_main calls this and passes my time delta

{
// physics logic

for(each object)
{
object[i].pos.x += object[i].vel.x*deltatime;
object[i].pos.y += object[i].vel.y*deltatime;
// etc //

}

// blitting

for(each object)
{
blit each object at object[i].pos.x>>10, objecti[i].pos.y>>10
}
}


See what I do is assume the time delta is a 22.10 fixed point number. This works because my object's positions are also in 22.10 fixed format. When I go to render the objects, I right shift their positions by >>10 to get the whole portion, and this is where I blit them. What's cool is that internally, I get 1/1024 subpixel precision, without using floating point. Velocities are just regular integers at the moment, where 1 = 1 pixel per second. In reality you can setup positions, velocities and the time delta as fixed point numbers, with any number of fractional bits you want, just ensure you don't lose too much precision due to right shifting, and don't overflow when multiplying. I am actually going to give my velocities 6 bits of fractional precision, I'll just have to right shift my positions by >>16 when blitting, to compensate for it. It works.
well this is from the "Interactive Game Making" Forum. but i really don't know what this guy is talking about. i did a google on it. and from i could tell Fixed Point is something like 34.23, 4.46, 0.09. Where as Floating Point is like 4.345, 4.3, 2.341, 2.0. But I don't understand how he's using it in that code above. Can someone body break it down to me 5 year old style ? Keeping the explanation related to the code above if possible. Thanks. edit: in case it's not clear. i understand what he doing in the code. i'm just don't understand how's using Fixed Point Math to do it. [edited by - Alpha_ProgDes on October 9, 2003 4:07:38 AM]

##### Share on other sites
an object moves a certain distance in amount of time in a direction. Deltatime is time pos is current position and vel is how fast your going in a certain direction. Thats what the physics logic does.

##### Share on other sites
The second part draws it.

##### Share on other sites
Fixed point maths is where you treat an integer as having a fractional part. The 22.10 fixed point number referred to is a 32 bit number - the top 22 bits are the "whole" part, and the bottom 10 bits are the fractional part.

Using this scheme, in binary, 10000000000 (0x400) is considered to be 1.0, 0x200 can be considered to be 0.5, and so on.

When drawing, the numbers are shifted right 10 places to remove the fractional part, which rounds the value down to the nearest whole number.

The 22.10 split is entirely up to you, depending on how you want to trade off range for accuracy. You could just as easily use 16.16 or 28.4, depending on your needs.

##### Share on other sites
Oh, and err, why?

Well, once upon a time, floating point was really slow so (just about) all games used fixed point maths. Nowadays, it''s a bit of a grey area. Some operations can be faster using integers, some slower, it really depends on your hardware.

One advantage of fixed point is that your accuracy remains consistent across the entire range of the numbers, whereas with floating point, this varies. A single precision floating point number can represent far more numbers between 0 and 1 than it can between 1,000,000 and 1,000,001. For encoding huge game worlds, it can often be better to use integer rather than floating point.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 10
• 11
• 37
• 12
• 12
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999958
×