jacob navia said:
P.J. Plauger a écrit :
After reading that proposal, I was struck by the fact that since quite a
long time we are turning around the problem of defining SUBTYPES for the
language.
Consider this:
int a;
and
volatile int a;
The second is a SUBTYPE of integer, i.e. an integer with special
characteristics, in this case that is volatile.
In the same manner, fixed point could be understood as a subtype of
double/float, etc, where the implementation changes.
[...]
No, fixed-point types are not subtypes of floating-point types, any
more than integer types are subtypes of floationg-point types.
If S is a subtype of type T, then surely every value of type S must
also be a value of type T. For example, in languages that support
subtypes, you might define an integer type with a range of
-32768..+32767, and a subtype of that type with a range of -99..+99.
A small fixed-point type might happen to have values that are a subset
of the values of a larger floating-point type, just as, for example,
every 32-bit integer value might be exactly representable in a 64-bit
floating-point type. But this is neither necessary nor useful. If a
given fixed-point type and a given floating-point type have the same
size (and no "padding bits"), each type will inevitably be able to
represent values that the other cannot. There is no subset
relationship.
(I put "padding bits" in quotation marks because the standard defines
the term only for integer types.)
Mathematically, the representable values of a fixed-point type are
less dense than those of a floating-point type for values near zero,
but more dense for values with larger magnitudes; fixed-point doesn't
"waste" bits on a variable scale factor (the exponent).