24

For intense number-crunching i'm considering using fixed point instead of floating point. Of course it'll matter how many bytes the fixed point type is in size, on what CPU it'll be running on, if i can use (for Intel) the MMX or SSE or whatever new things come up...

I'm wondering if these days when floating point runs faster than ever, is it ever worth considering fixed point? Are there general rules of thumb where we can say it'll matter by more than a few percent? What is the overview from 35,000 feet of numerical performance? (BTW, i'm assuming a general CPU as found in most computers, not DSP or specialized embedded systems.)

Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
DarenW
  • 16,549
  • 7
  • 63
  • 102

7 Answers7

18

It's still worth it. Floating point is faster than in the past, but fixed-point is also. And fixed is still the only way to go if you care about precision beyond that guaranteed by IEEE 754.

John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • 2
    +1, do you have any benchmarking software that you use to verify this for different architectures. I came to the same conclusion through profiling, but it was a fair bit of work and the results only relate to the specific hardware I've tested. Interested in seeing the difference on mobile devices with ARM as well as the more common Intel and AMD. – SmacL Jun 09 '11 at 12:20
16

In situations where you are dealing with very large amounts of data, fixed point can be twice as memory efficient, e.g. a four byte long integer as opposed to an eight byte double. A technique often used in large geospatial datasets is to reduce all the data to a common origin, such that the most significant bits can be disposed of, and work with fixed point integers for the rest. Floating point is only important if the point does actually float, i.e. you are dealing with a very wide range of numbers at very high accuracy.

SmacL
  • 22,555
  • 12
  • 95
  • 149
  • 5
    Great comment about the "floating" part of floating point - blew my mind wide open, and so obvious in retropect. – Curyous Mar 23 '11 at 01:42
10

Another good reason to use fixed decimal is that rounding is much simpler and predictable. Most of the financial software uses fixed point arbitrary precision decimals with half-even rounding to represent money.

ddimitrov
  • 3,293
  • 3
  • 31
  • 46
  • Very true. This also applies when implementing a deterministic, distributed real-time simulation. Subtle differences in IEEE-754 implementations can result in extremely hard to find bugs where the same calculation has a different result on different distributed systems, causing them to diverge from the expected simulation state. See [this article](http://gafferongames.com/networking-for-game-programmers/floating-point-determinism/). – Engineer Apr 10 '15 at 14:11
  • Fixed point arbitrary precision is a contradiction – Frank Oct 24 '22 at 20:55
5

Its nearly ALWAYS faster to use fixed point (experience of x86, pentium, 68k and ARM). It can, though, also depend on the application type. For graphics programming (one of my main uses of fixed point) I've been able to optimize the code using prebuilt cosine tables, log tables etc. But also the basic mathematical operations have also proven faster.

A comment on financial software. It was said in an earlier answer that fixed point is useful for financial calculations. In my own experience (development of large treasury management system and extensive experience of credit card processing) I would NOT use fixed point. You will have rounding errors using either floating or fixed point. We always use whole amounts to represent monetary amounts, counting the minimum amount possible (1c for Euro or dollar). This ensure no partial amounts are ever lost. When doing complex calculations values are converted to doubles, application specific rounding rules are applied and results are converted back to whole numbers.

Tim Ring
  • 1,845
  • 1
  • 20
  • 27
  • In my case (commissions calculation) we use Java BigDecimal, which is not exactly a fixed point, but comes pretty close. The rounding rules are indeed application specific, but 9 times out of 10 they are half-even. – ddimitrov Oct 05 '08 at 13:28
  • By complex calculations, you are meaning compounding interest, tax rates or some such? For adding and subtracting amounts in an account, would not fixed be pretty much the One True Way? Do you use floats in that case? – DarenW Oct 05 '08 at 23:42
  • 1
    Table lookup is an important consideration in what i'm doing, and what many others may need - this is not practical with floating point. A very good point. – DarenW Oct 05 '08 at 23:48
4

Use fixed-point when the hardware doesn't support floating-point or the hardware implementation sucks.

Also beware when making classes for it. Something you think would be quick could actually turn out to be a dog when it comes to profiling due to (un)necessary copies of classes. That is another question for another time however.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
  • It would be interesting to know what hardware doesn't support floating point these days. It seems that now, even a ratty old broken shoe lace does floating point... 8P – DarenW Oct 05 '08 at 23:44
  • 1
    (btw, i wear sandals here in Florida, so i'm not up on shoelace technology) – DarenW Oct 05 '08 at 23:44
  • 2
    @DarenW: The Nintendo DS (which I still consider somewhat new) doesn't support floating point via hardware. That's the only handheld device I've developed for, but I'd guess that many other handhelds don't have an FPU either. – Ponkadoodle Feb 07 '10 at 03:11
  • 1
    Recently I ran into some business with the Blackfin ARM processor which apparently doesn't support floating point arithmetic in hardware and does such calculations in software, which is quite expensive. – neuviemeporte Nov 05 '10 at 17:27
  • 1
    Many DSPs (digital signal processors) do not support floating point. – Tor Klingberg Apr 01 '15 at 13:49
  • @graham.reeds Or preferably, just use C functions to avoid any potential overheads. – Engineer Apr 10 '15 at 15:38
1

Another reason to use fixed-point is that ARM devices, like mobile phones and tablets, lack of FPU (at least many of them).

For developing real-time applications it makes sense to optimize functions using fixed-point arithmetic. There are implementations of FFTs (Fast Fourier Transform), very importan for graphics, that base its improvements on efficiency on relying on floating point arithmetic.

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
0

Since you are using a general-purpose CPU, I would suggest not using fixed point, unless performance is so critical for your application that you have to count every tic. The hassle of implementing fixed point, and dealing with issues like overflow is just not worth it, when you have a CPU, which will do it for you.

IMHO, fixed point is only necessary when you are using a DSP without hardware support for floating point operations.

Dima
  • 38,860
  • 14
  • 75
  • 115