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

- Sjoerd van Leent (17/17) Nov 30 2004 For many environments it isn't necessary to have floating-point
- Holger Sebert (7/31) Nov 30 2004 (sorry, Sjoerd, I think I just sent you an email instead of posting this...
- Sjoerd van Leent (16/23) Dec 01 2004 I'll try to explain this. If you've a float with number 0.0 and a float
- Lionello Lunesu (1/1) Dec 02 2004 1.0 :-)
- Lionello Lunesu (4/7) Nov 30 2004 It's fairly easy to make a fixed-point template class with all operators...
- Sjoerd van Leent (5/18) Dec 01 2004 I doesn't have to be a built-in type. It may be just as well a template

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, SjoerdFixed 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.HolgerRegards, 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..I doesn't have to be a built-in type. It may be just as well a template type of course :-). Regards, SjoerdFixed-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.

Dec 01 2004