www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - For v2.0 Fixed Point Arithmetic

reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
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
next sibling parent reply Holger Sebert <holger.sebert ruhr-uni-bochum.de> writes:
(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
parent reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
Holger Sebert wrote:
 
 Fixed point arithmetic tends to be *slower* than floating point 
 arithmetics on modern machines.

This is true
 
 I 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
parent "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
1.0 :-) 
Dec 02 2004
prev sibling parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
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
parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
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