## digitalmars.D - For v2.0 Fixed Point Arithmetic

- Sjoerd van Leent <svanleent wanadoo.nl> Nov 30 2004
- Holger Sebert <holger.sebert ruhr-uni-bochum.de> Nov 30 2004
- Sjoerd van Leent <svanleent wanadoo.nl> Dec 01 2004
- "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> Nov 30 2004
- Sjoerd van Leent <svanleent wanadoo.nl> Dec 01 2004

For many environments it isn't necessary to have floating-point arithmetic. Sometimes it isn't even preferred. There are two solutions, using integral numbers or using fixed-point numbers. Fixed-point arithmetic is normally faster, and, is more stable as floating point numbers because it uses sets of integral numbers to define one fixed-point number. Examples of a possible implementation: fixed f = 10.000; // Has 32-bit integer at the left side, // and a 32-bit integer at the right // side. typedef fixed64 fixed(64,64); fixed64 f2 = 10.000; // Same only with 64 bits at both sides fixed(16,16) f3 = 10.000; // Same only with 16 bits at both sides I hit this while reading the introduction of CUJ, and think that it might be useful for applications who need stability over quantity. Regards, Sjoerd

Nov 30 2004

(sorry, Sjoerd, I think I just sent you an email instead of posting this ...) Sjoerd van Leent wrote:For many environments it isn't necessary to have floating-point arithmetic. Sometimes it isn't even preferred. There are two solutions, using integral numbers or using fixed-point numbers. Fixed-point arithmetic is normally faster, and, is more stable as floating point numbers because it uses sets of integral numbers to define one fixed-point number. Examples of a possible implementation: fixed f = 10.000; // Has 32-bit integer at the left side, // and a 32-bit integer at the right // side. typedef fixed64 fixed(64,64); fixed64 f2 = 10.000; // Same only with 64 bits at both sides fixed(16,16) f3 = 10.000; // Same only with 16 bits at both sides I hit this while reading the introduction of CUJ, and think that it might be useful for applications who need stability over quantity. Regards, Sjoerd

Fixed point arithmetic tends to be *slower* than floating point arithmetics on modern machines. I also can't quite see why fixed point arithmetic is more stable ... Holger

Nov 30 2004

Holger Sebert wrote:Fixed point arithmetic tends to be *slower* than floating point arithmetics on modern machines.

This is trueI also can't quite see why fixed point arithmetic is more stable ...

I'll try to explain this. If you've a float with number 0.0 and a float with the outcome of 5.0 divided by 5.0: float float1 = 0.0; float float2 = 5.0/5.0; It is not safe to say: float1 == float2; As you might know, floating-point numbers are not safe in such equations. However, fixed-point numbers are safe. That is why I said that fixed-point arithmetic is more stable as floating-point arithmetic. Of course, since floating-point arithmetic is more fine grained then ever before, it is very well possible to say that it is becoming more stable. But it is not the most stable.Holger

Regards, Sjoerd

Dec 01 2004

Hi..Fixed-point arithmetic is normally faster, and, is more stable as floating point numbers because it uses sets of integral numbers to define one fixed-point number.

It's fairly easy to make a fixed-point template class with all operators overloaded. Why would it have to be a built-in type? Lionello.

Nov 30 2004

Lionello Lunesu wrote:Hi..Fixed-point arithmetic is normally faster, and, is more stable as floating point numbers because it uses sets of integral numbers to define one fixed-point number.

It's fairly easy to make a fixed-point template class with all operators overloaded. Why would it have to be a built-in type? Lionello.

I doesn't have to be a built-in type. It may be just as well a template type of course :-). Regards, Sjoerd

Dec 01 2004