karthikbalaguru said:
Interesting !!
Do you mean to say that C does not have facility for exact Floating
Point
number validation or processing irrespective of the type of
processor ?
The problem is not specific to C; it's inherent in all floating point
arithmetic. The fundamental problem is that a floating point type
containing a fixed, finite number of bits can represent only a finite
number of different numeric values. MOST floating point operations will
produce a value that cannot be represented exactly, the best that they
can do is produce the closest representable value, and in many cases
it's not even feasible to guarantee that (consider, for instance,
sin(DBL_MAX)).
Since most floating point operations introduce a certain amount of
error, it's generally not a wise idea to compare floating point values
for exact equality.
That is something unexpected from the C Language. I thought
it would do comparison operations correctly !!
5.2.4.2.2p5: "The accuracy of the floating-point operations (+, -, *, /)
and of the library functions in <math.h> and <complex.h> that return
floating-point results is implementation defined, as is the accuracy of
the conversion between floating-point internal representations and
string representations performed by the library functions in <stdio.h>,
<stdlib.h>, and <wchar.h>. The implementation may state that the
accuracy is unknown."
Note that this allows both for an arbitrarily inaccurate floating point
implementation (such that -DBL_MAX == DBL_MAX), so long as the
implementation documents that "== performs comparisons on doubles with
an accuracy of +/-2*DBL_MAX". However, the same exact wording also
allows an implementation that achieves exactly the maximum possible
degree of accuracy allowed within the limitations I've mentioned above.
Note that if a C99 implementation pre-defines the __STDC_IEC_559__
macro, than the requirements of IEEE/IEC 60559 apply, which gives you a
lot of guarantees that the C standard itself does not provide.
While the C standard does not specify the accuracy of floating point
operations, in C99 it added a macro in <float.h> named FLT_EVAL_METHOD
which give you some information about what accuracy an implementation
actually provides, and a #pragma named STDC FP_CONTRACT in <math.h> that
gives you some control over the the accuracy.