views:

828

answers:

6

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.)

+12  A: 

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
A: 

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
+4  A: 

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
my interest wasn't financial but this is a good point
DarenW
+2  A: 

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
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
(btw, i wear sandals here in Florida, so i'm not up on shoelace technology)
DarenW
@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.
Wallacoloo
+2  A: 

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
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
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
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
+5  A: 

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.

Shane MacLaughlin