Numeric Types in Fine
These are my current thoughts on how I would like numeric types to be
in Fine:
Each numeric class will have the following features predefined:
- Minimum - the lowest value (toward "-" infinity) that this
number can contain.
- Maximum - the highest value (toward "+" infinity) that
this number can contain.
- Size_in_bits - the number of bits that are used to implement
this type (would Number_of_bits be better?).
- Size_in_bytes - the number of bytes that are used to implement
this type (would Number_of_bytes be better?).
Note: Size_in_bits is not necessarily Size_in_bytes * 8. See the
INTEGER N bits type, below.
Integer types:
- INTEGER - this is the same as the INTEGER class in Eiffel and is
implemented as a C int. It is
likely to be 32 bits on most machines, although I have used
systems where a C int was 16 bits and there are machines where an
integer be 18, 24, or 36 bits.
- INTEGER range N..N - this is an integer that is range limited.
When a value is assigned to it, it is checked to verify that the
number is within the range N..N. As if the class had an invariant
clause that read: Minimum <= Current and Current <= Maximum.
The compiler should choose the smallest implementation for this
type that will work on the target system (i.e. if the range was
-100..100 the value could be contained in a byte. And if the
range is >=0..N the value will be implemented as an unsigned.
- INTEGER N bits - this would be similar to the BIT_N class in
Eiffel, where the integer can be defined to be a specific number
of bits. It should be such that N does not have to be an even
power of 2 (i.e. 8, 16, 32, 64, etc.). And if it is not an
even power of 2 the compiler should limit the value to that size
after each operation.
It should also be able to generate numbers larger
than the largest word size available for the hardware (i.e.
INTEGER 512) and have the compiler perform the operations.
(This would be easier if the compiler could generate assembly
language. Could we get tricky and have the COMPILER class
know how to generate assembly for particular machines?)
- INTEGER unsigned N bits - same as above, but unsigned.
- INTEGER like "C" - to allow easier interfacing to C or
C++. These should have limited use and perhaps should be limited
to being used inside of a "wrapper" class?
- It would be nice if the integer classes had a require or insure
clause on the prefix "-" feature, that checked to verify it wasn't
(or didn't) negate the Minimum value and therefore is returning
the same value. I.E. -(-2147483648) -> -2147483648.
Floating point types:
- FLOAT - this is the same as the REAL class in Eiffel and is
implemented as a C float. It really could be any size, but it
is likely to be a 32-bit float on most machines.
- FLOAT range N.N..N.N - this is a float that is range limited.
When a value is assigned to it, it is checked to verify that the
number is within the range specified. As if the class had an
invariant clause that read: Minimum <= Current and Current <=
Maximum.
The compiler should choose a double if the value is too large
for a float.
- FLOAT N bits - this allows the size of the variable to be
specified. Probably restricted to 32 or 64 (or whatever the
machine size is. It would be nice if somehow the 80 bit floating
numbers in some machines could be accessed... maybe if the
compiler could generate inline assmebly language some way?
- FLOAT accurate N bits - would it be good to be able to specify
the minimum number of bits of accuracy??
- REAL - a pseudonym for FLOAT (or FLOAT 32?) for upward
compatability with Eiffel.
- DOUBLE - a pseudonym for FLOAT 64.
Angle types:
- ANGLE - this is really just a floating number of the most natural
type. So normally it will be a double float in radians. But on
systems that are say limited to 32 bit floating point, they can
be defined to be floats. It will
have the features like sine, cosign, tangent, etc. defined.
- ANGLE range N..N - this is an angle that is range limited.
When a value is assigned to it, it will be converted to the range
specified. I.E. if the range specified is 0..2*Pi and the value
-Pi is assigned, the value is converted to Pi.
- ANGLE N bits - this would create an integer angle (BAM - Binary
Angular Measurement?). Their range would be -180 to 179.999...
degrees. They automatically wrap and can be
converted to regular angles. Possibly the features like sine,
cosine, etc. can be implemented without converting to double
so that they could be used on machines without floating point
hardware.
- ANGLE unsigned N bits - same as above but the range would be
0 to 359.999... degrees.
- ANGLE_DEGREES - same as ANGLE, but in degrees.
Fixed point types:
- FIXED - this is the same as an INTEGER, i.e. it is a fixed
point value with the binary point all the way to the right.
- FIXED Nib.Nfb - this is a fixed point number with Nib integer
bits and Nfb fraction bits. One of the Nib bits is assumed to
be the sign bit. The compiler should adjust the scale
automatically for the normal math operations, i.e. if two
FIXED 23.9 values are multiplied the result is shifted back
to a FIXED 23.9 value. If a fixed value is declared 0.N
it is assumed that it is a fractional value with a range of
-1.0 to 0.9999... .
- FIXED unsigned Nib.Nfb - this is the same as above but unsigned.
If it is declared 0.N it will be a fractional only value from
0.0 to 0.9999... .
Your suggestions, comments, etc. are most welcome, please e-mail me at
sedwards@xmission.com.
Thanks to everyone who has sent me their comments. I will be
incorporating some changes into this document, as soon as I have
finished evaluating them.
Copyright (C) 2001 - J. Scott Edwards - sedwards@xmission.com
- http://fine.sourceforge.net
You are hereby granted permission to copy this information, in
whole or in part, as long as this copyright notice is retained.
Updated: 9-Mar-2001
|