Fixed Point Maths Explained - Retro Programming

preview_player
Показать описание
A video explaining how fixed point maths works and why it is useful on CPUs that have no floating point units.

A written version of this is available on my website, linked below

Fixed point maths is a way to deal with decimal numbers, on systems where they don't have the ability to use decimals. The basic concept is to scale all numbers up by a certain amount to remove any decimal parts of the number. Then you can work with whole numbers only, scaling back down when needed.

What scale you use depends on the range of numbers needed for a particular situation. It can also depend on what the hardware is best at managing. A Z80 CPU for example can work with 16 bit values reasonably easily. And if you’re not trying to do complex accurate scientific calculations, the resolution of your machine’s screen is likely to be a factor as well.

But if you want a quick one to use, this explanation uses what is called 8.8 fixed numbers. That is a 16 bit value where the upper 8 bits are the whole number and the lower 8 bits the fractional part. With an imaginary binary point in the middle.

Chapters

00:17 Maths is hard
00:56 Floating point maths
01:45 I have more videos!
03:53 What we're trying to do
04:57 Example with money
06:39 Introducing fixed point maths
08:28 negative numbers
10:39 Explaining fixed point
15:03 Summary
Рекомендации по теме
Комментарии
Автор

Fun stuff - subbed. I stumbled upon this as I'm writing some fixed-point for an ATMega328 microcontroller. I'm using "Q15.16" format in this case, in my motor control code. It is way faster than floats, especially when I also approximate the sine function using parabolas. Keep up the good work; will check out what else is on your channel.

agnichatian
Автор

Really impressed with the presentation and professional set up, the clear work on squared paper and it's well explained. Definitely heading into a level of explanatory quality that a certain UK university that specialises in computer files might put out :D

Looking forward to the next coding video :D

arronshutt
Автор

There is a much simpler scheme (at least in concept). JUST DON'T DO FRACTIONS AT ALL! How to do this & get reasonable accuracy? Up front, declare a small unit that is "smaller than max permitted calculation error" & do all calculations in integer quantities of that unit. In game, you may choose say 1/10000th (or maybe better 1/2^n) of the playing field's dimensions. There is no "fixed point" (unless you consider the one right of the LSB the point). For the money example, let the quantities be in pence.
When units are dimensionless (such as angle), choose unit that is 1/(2^n) of a full rotation. You simply ignore all carries beyond the nth bit when doing that rotation math.
This "retro math" should not be retro! For example in computer aided design, integer meth should be used. Floating point math is nothing but trouble. It is very important in CAD that tests for equality work (is the cursor EXACTLY on that "handle"? Is that line & arc "connected"?) Floating point causes quantities that can never be "equal" so the thing you just placed, you can never "grab" again because you can't get the cursor EXACTLY on it. There is also the famous problem of "leakage" in PC Paint app with the paint roller, because line endpoints don't exactly match up. Endpoints of arcs other than 0°/90°/180°270° can't be exactly on line endpoints, if they are specified by center location, radius, & start/end angles. The floating point "solution" is to add "slop" in the calculation (if abs(A - B) < some small number then...). The slop "is small enough unless it is too small". If I move something around in CAD workspace, its (X, Y, Z) parameters change in precision!
And no, fixed point or integer math CAN & SHOULD be used for trajectory calculations! The problem with floating point is that it has resolution that differs with the distance from zero. If I put (0, 0, 0) at the center of the sun, what about when I am near Jupiter? Resolution & precision will degrade! Now I have a system with precision that depends upon where I choose (0, 0, 0).
Floating point is for quick scientific calculations for those too lazy to properly scale their units.

bpark
Автор

The link to your website in the video description is down, do you have a different link for the same post or info?

heavymetalmixer
Автор

One minor correction, the screen is 320 pixels across... You're using a mode that only uses 256 pixels across... :-P And that's before we discuss the half-width pixel mode... :-D

-Dx

Xalior
Автор

How would this apply in creating a world like Nintendo 64 if there was no FPU? What if you had a very fast powerful fixed point DSP instead? Could you make a Mario 64 or better yet the faster Mario kart 64? Thank you for the great video 😀

valentine_puppy
Автор

Enjoyed this. Look for the cheats! Look up tables.

jonwest
Автор

I want to make a 3D engine that has a ps1 aesthetic so this is essential for me.

Tabu
Автор

To be pedantic representing negative in binary isn't inherently different to decimal. I could write -01011010.

The computer representation of a negative is different to how we write it. Well it can be, and generally is in practice.

benholroyd