We've just updated MediaWiki and its underlying software. If anything doesn't look or work quite right, please mention it to us. --RanAS

Fixed point

From SnesLab
Jump to: navigation, search

The SNES's processor can only natively operate on integers, and doesn't support floating point numbers. Fixed-point math is a way to get fractional values using only integer operations.

For a fixed point number, you treat some amount of the smallest bits as being fractional. The name comes from having an imaginary "decimal point" inside the number, that always has the same number of bits before and after it; in other words it's in a fixed position.

Math

For math between fixed point numbers with the same number of fractional bits, addition, subtraction, bit operations (AND, OR, XOR), and comparisons are done the same as for regular integers. The only special handling is the extra step of converting them to integers with a shift when an integer is needed.

Multiplication and division

Unlike more basic math, multiplication and division between fixed point numbers result in numbers that have a different number of fractional bits than the inputs. Because of this, some compensation is required if you want to keep the result in the same format.

For multiplication, you shift the result right by however many fractional bits you have after you do the multiplication.

For division, you shift the numerator left by however many fractional bits you have before you do the division.

Conversion to/from integers

To convert a fixed point number to an integer (perhaps to get a number of pixels for sprite coordinates), you just shift right for as many bits as you have fractional bits. To convert an integer to a fixed point number, you do the reverse and shift left for however many fractional bits you have.