Skip to main content
GameDev.net gamedev.net
🔒 Locked

fixed?

Started by programering Jan 9, 2004 at 9:09 AM 8 replies 650+ views
Original Post
programering
programering
I didn''t find "fixed" in the Game Dictionary so I ask it here? What is it with fixed values, what''s the different between fixed and ordinary values? Is fixed a constant integer value? Thanks in advance! ------------------------------- Anton Karlsson Klingis Entertainment Games with silly humor Aleph One (Marathon Open Source) | My Homepage Packa bajs med winzip! Just dreaming wont make you (more) skilled in game programming/development.
Endurion
Endurion
Are you referring to fixed point numbers?

Fixed point numbers, as opposed to floating point numbers, are a way to store non-integer numbers in an integer. While floating point numbers can have a "floating point", meaning you have a value of 1.2345 and an offset for the point like 6 numbers "up" or "down" fixed point numbers are very restricted in their range.

There is also no specified way how fixed point numbers are to be interpreted, this depends on the particular implementation.

Pro of fixed point numbers:
-no in-between numbers/rounding errors, a value is 0 or not, while floating point numbers need to be checked with a very small offset (fabs( floatvalue - 0 ) < eps ).

Cons
-no particular definition, needs to be implemented by yourself


Feel free to flame away...
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
shurcool
shurcool
Are you saying...
float fTest = 0.0f;cout << (fTest == 0.0f) ? "Is 0." : "Isn''t 0." << endl; 
Will return "Isn''t 0"? I don''t see why 0.0 * 10^0 can''t be stored in a float. Could someone please explain the reasoning behind that? :|

---
shurcooL`
pjcast
pjcast
assigning a direct value to a float, such as:

float f = 1.2f;

that will always equal 1.2f

However, when you do any kind of manipulation to that, there will be a slight deviation from what you expect (due to compiler/os/fpu quirks). Which is where the epsilon comes into play.

Fixed point math **was** used extensively in graphics to avoid costly floating point calculations.
superpig
superpig
Let's see if I remember all this...

Fixed point numbers are, as Endurion said, a way of representing floating point numbers ('real' numbers) without actually using floating point math. Aside from the pros and cons that Endurion mentioned, there's also the possibility that the system you're coding for simply doesn't have a floating point math processor - the GBA is a prominent example, floating point math is very slow because there's no dedicated hardware for it.

You're probably aware of how regular binary numbers work. Say you've got a 4-bit number of value 11:


-----------------
| 8 | 4 | 2 | 1 |
|---|---|---|---|
| 1 | 0 | 1 | 1 |
-----------------

Larger numbers can be expressed by extending the sequence upwards to the left; the value of the bit in that position doubles each time, 1*2=2, 2*2=4, 4*2=8, etc...

But what if we look at it the other way? Reading from left to right, the numbers would seem to halve each time. And so if we extend the table to the right instead of the left:


---------------------------------------
| 8 | 4 | 2 | 1 |0.5|0.25|0.125|0.0625|
|---|---|---|---|---|----|-----|------|
| 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 |
---------------------------------------


Now the number being represented is 11 + 0.5*1 + 0.125*1 = 11.625. And yet it's small enough to fit in a char variable. To multiply the number by two, you left-shift it; to divide by two, you right-shift. Both of those are very fast.

Because there's no standard rule about how many bits should be used to represent the integral part of the number, and how many should be used to represent the fractional part, you get to pick. The more bits you use for the fractional part, the more precise the number can be; conversely, the more bits you use for the integral part, the larger the number can be.

The GBA dictates the fixed point format that you use for particular things (e.g. sprite rotations). Personally, I like to use a ratio-style notation for describing fixed point; the example I showed above I'd say was 4:4 fixed point. You'll probably come across 8:8, 12:4, 4:12, 24:8... all sorts. You need to pick your format based on the types of numbers you expect to be needing to store.

Richard "Superpig" Fine
- saving pigs from untimely fates, and when he's not doing that, runs The Binary Refinery.
Enginuity1 | Enginuity2 | Enginuity3 | Enginuity4 | Enginuity5
ry. .ibu cy. .y'ybu. .abu ry. dy. "sy. .ubu py. .ebu ry. py. .ibu gy." fy. .ibu ny. .ebu
"Don't document your code; code your documentation." -me
"I'm not a complete idiot; parts of me are missing." -}}i{{


[edited by - Superpig on January 9, 2004 8:49:38 PM]
Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse
shurcool
shurcool
Good explanation, Richard.

BTW, shifting right (>>) will always double a number, no matter what the endianness of that particular machine is, no? :o And Windows/x86 machines are always little-endian, right?

quote:
Original post by pjcast
However, when you do any kind of manipulation to that, there will be a slight deviation from what you expect (due to compiler/os/fpu quirks). Which is where the epsilon comes into play.


Ah, yes, I remember now. This happened to me in Macromedia Flash. I had a sprite, that had a rotation variable; pressing arrow keys would add/subtract 4.5 degrees (for example). After going around in a complete circle (4.5 * 80 = 360), the variable would be something like 0.000000000026311269.

---
shurcooL`
programering
programering
Why is the fractional bits slower? Can someone explain that for me?

-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Aleph One (Marathon Open Source) | My Homepage

Packa bajs med winzip!
Poo Figthers
Good Harlot
CPU Burn out
programering
programering
I have done some research on my own, figuring out the fractional bits placement for 0.1 with binary dividing (1/10).

| 8 | 4 | 2 | 1 |
|---|---|---|---|
| 0 | 0 | 0 | 1 | 1
|---|---|---|---| -- = 0
| 1 | 0 | 1 | 0 | 10
|---|---|---|---|
| 0 | 0 | 0 | 0 |
|---|---|---|---|

| 4 | 2 | 1 |0.5|
|---|---|---|---|
| 0 | 0 | 1 | 0 | 1
|---|---|---|---|--- = 0
| 1 | 0 | 1 | 0 | 5
|---|---|---|---|
| 0 | 0 | 0 | 0 |
|---|---|---|---|

| 2 | 1 |0.5|0.25
|---|---|---|---|
| 0 | 1 | 0 | 0 | 1
|---|---|---|---|--- = 0
| 1 | 0 | 1 | 0 |2.5
|---|---|---|---|
| 0 | 0 | 0 | 0 |
|---|---|---|---|

| 1 |0.5|0.25|0.125
|---|---|----|----|
| 1 | 0 | 0- | 0- | 1
|---|---|----|----|---- = 0
| 1 | 0 | 1- | 0- |1.25
|---|---|----|----|
| 0 | 0 | 0- | 0- |
|---|---|----|----|

| 1 |0.5|0.25|0.125
|---|---|----|----|
| 1 | 0 | 0- | 0- | 1
|---|---|----|----|----- = 1
| 0 | 1 | 0- | 1- |0.625
|---|---|----|----|
| 0 | 0 | 0- | 1- | goes one time
|---|---|----|----|

binary subtracting:
| 0 | 1 | 1 | 1 |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
|---|---|---|---| the 1 are taken
| 0 | 1 | 0 | 1 |

| 0 | 1 | 1 | 1 |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
|---|---|---|---| two bits left over
| 0 | 1 | 0 | 1 |
|---|---|---|---|
| 0 | 0 | 1 | 1 |

|0.5|0.25|0.125|0.0625
|---|----|-----|-----|
| 0 | 1- | -1- | -0- | 0.375
|---|----|-----|-----|------- = 1
| 0 | 1- | -0- | -1- | 0.3125
|---|----|-----|-----|
| 0 | 0- | -1- | -1- | goes again
|---|----|-----|-----|

|0.25|0.125|0.0625|0.03125|
|----|-----|------|-------|
| 0- | -0- | -1-- | --0-- | 0.0625
|----|-----|------|-------| ------ = 0
| 0- | -1- | -0-- | --1-- | 0.15625
|----|-----|------|-------|
| 0- | -1- | -1-- | --0-- |
|----|-----|------|-------|

Now we are back on this step again:

| 2 | 1 |0.5|0.25
|---|---|---|---|
| 0 | 1 | 0 | 0 | 1
|---|---|---|---|--- = 0
| 1 | 0 | 1 | 0 |2.5
|---|---|---|---|
| 0 | 0 | 0 | 0 |
|---|---|---|---|

Because of the bits never matches eachother completly it will go on forever 0.001100110011001100110011001100110011........., never be exactly, just getting more and more exact just like 10/3=3.3333333333333333333333333...........


-------------------------------
Anton Karlsson
Klingis Entertainment
Games with silly humor

Aleph One (Marathon Open Source) | My Homepage

Packa bajs med winzip!
Poo Figthers
Good Harlot
CPU Burn out

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.