S
Shao Miller
Systems that use a logarithmic number system:
http://en.wikipedia.org/wiki/Logarithmic_number_system
Thanks. Can such a system implement C? I thought C floating-point
required a precise zero.
Systems that use a logarithmic number system:
http://en.wikipedia.org/wiki/Logarithmic_number_system
Ben Bacarisse said:army1987 said:Is this guaranteed to always work in all conforming implementations?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double zero = 0.;
if (zero == 0.) {
puts("Okay!");
return 0;
} else {
puts("Uh-oh!");
return EXIT_FAILURE;
}
}
The thread seems to have gone off on some tangent about operations
that are not involved here so I'll just stick these marks "out
there" in case they are useful.
The model that C uses for floating point means that all conforming
implementations must have at least one exact representation for zero.
[snip unrelated] [from another posting:] The C standard lays out a
model for how floating point types must be represented, and zero can
always be represented exactly.
Ben Bacarisse said:army1987 said:Is this guaranteed to always work in all conforming implementations?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double zero = 0.;
if (zero == 0.) {
puts("Okay!");
return 0;
} else {
puts("Uh-oh!");
return EXIT_FAILURE;
}
}
The thread seems to have gone off on some tangent about operations
that are not involved here so I'll just stick these marks "out
there" in case they are useful.
The model that C uses for floating point means that all conforming
implementations must have at least one exact representation for zero.
[snip unrelated] [from another posting:] The C standard lays out a
model for how floating point types must be represented, and zero can
always be represented exactly.
This comment is wrong in its facts. The Standard does give a
model for floating-point values, but that model pertains only to
defining the characteristics of floating-point types, not their
representations. There is no requirement that how floating-point
numbers are represented, or what values are representable, be
faithful to that model. There is no stipulation that zero must
be representable; neither does it follow as a consequence of any
other parts of 5.2.4.2.2. Moreover, note that section 5.2.4.2.1,
discussing integer types, includes a forward reference to
"representations of types (6.2.6)", whereas 5.2.4.2.2 gives no
such reference. 6.2.6.1 p1 makes the point explicitly: "The
representations of all types are unspecified except as stated in
this subclause." 6.2.6 contains no mention of floating-point
types.
Thanks. Can such a system implement C? I thought C floating-point
required a precise zero.
The C Rationale has in its discussion of:
5.2.4.2.2 Characteristics of floating types <float.h>
Note that the floating-point model adopted permits all common
representations, including sign-magnitude and two's-complement,
but precludes a logarithmic implementation.
So, the intent of the C89 committee was to require the value zero to
be representable.
(snip someone wrote)
I suppose, but we have had various questions about the possibility
of some hardware feature being legal in C.
Now, if someone actually has such a machine and wants to write a
C compiler, do you tell them that they can't do it?
Maybe floating point isn't needed for the needed problems, but
you say that they can't do it.
But you can look at it the other way. The requirement of C should
discourage hardware designers from designing such machines.
Still, if you find one what do you do?
Work around it?
If you forfeit a single value from the set of allowable
values and call it zero, then ensure the zero-and-non-zero C semantics
with special consideration to this representation, there is already an
excuse for the arithmetic results being slightly off.
(snip someone wrote)
I suppose, but we have had various questions about the possibility
of some hardware feature being legal in C.
Now, if someone actually has such a machine and wants to write a
C compiler, do you tell them that they can't do it?
Maybe floating point isn't needed for the needed problems, but
you say that they can't do it.
(snip, someone wrote)
Software emulation for all the floating point operations?
And then the values will be wrong if you pass them to other API
routines, or write them to files to be read by other programs.
I suppose you could call the smallest value 'zero', but it won't
compare equal to its negative.
Shao Miller said:What I meant was that there are certain semantics which require zero and
non-zero, and perhaps the implementation could insert a little extra
assembly... Suppose we have 'double_zero == 0'. Then the assembly
could actually compare the value of 'double_zero' with the forfeited
value that's called zero. In fact, it could do little else, if there's
no actual zero.
Comparing equal to zero seems a little more trivial than performing
arithmetic, so arithmetic could just be performed "naturally." If I
recall correctly, expectations for floating-point arithmetic in C are
already that it's potentially imprecise (up to the implementation). I
don't know of a reason why the zero and non-zero semantics (such as
comparison) need to be imprecise.
I think just about *every* floating-point operation (except
assignment) would have to take this fake "zero" into account.
It wouldn't just have to compare equal to 0.0, it would have to
compare less than all positive values and greater than all negative
values, x+0 and x-0 would have to yield x, x*0 would have to yield
0, 0/x would have to yield 0, and so on.
Unless you take unreasonable advantage of C's vague requirements
for floating-point semantics -- but then I'm not sure it's worth
the effort, since that's likely to give you mathematically incorrect
answers in a lot of cases.
If you can make your "fake zero" a trap value (not just in the
C sense of a trap representation, but something you can actually
*trap*), you can do after-the-fact cleanup if you use the fake zero
in an expression.
Practically speaking, it's not worth worrying about until somebody
comes up with an actual system whose floating-point can't represent
0.0 and wants to implement C on it.
Shao Miller said:On 3/1/2013 13:07, Keith Thompson wrote:
(snip)
Well that's what I was saying. The arithmetic operators are "allowed"
to give imprecise results anyway, so 'y = 0.; x == x - y' needn't yield
1.
I cannot imagine 'y == y' (or 0.) being too complicated.
That'd be handy.
Agreed.
Shao Miller said:That'd be handy.
Palmer and Morse (the lead architects on the 8087) talked about it in
their book on the coprocessor.
Basically the original idea was to
have the coprocessor spill to memory automatically, but that was
dropped for die space issues, and the idea was then to allow a trap to
handle the spills (and fills). While the facility was at least half
there, it turned out to be impossible to reliably tell why the fault
occurred (stack overflow and underflow are not reliably distinguished
from other errors), and then the restart would have been very
expensive, since the 8087 instruction could not actually have been
restarted, the faulting instruction would have had to have been
emulated in software.
And then it never got fixed.
As far as I know, the math parts of the 8087, 80187 and 80287 were
basically identical, just the bus interfaces varied. The extra
overhead on the '286 (because the busses were not well matched) was
such that at the same clock speed you'd get slower performance from a
286/287 combination than an 8086/87. The 80387 was a major revamp.
I've always assumed that "floating point operations" was the key phrase,
and that "(+, -, *, /)" should be taken only as examples,
ditto.
implying, in
particular, that the relational and equality operators were also
intended to be covered by that clause.
On the other hand, You might be right. If so, does that mean that the
unary +, unary -, !, ?:, ++, --, and compound assignment operators
acting on floating point values are also not covered,
Nit: fetch of the variable and 'decay' of a string literal value likeWhich do not appear in the code.
Which do not appear in the code.
I do not see conversions happening in the code.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.