G
Guest
Mark McIntyre ha scritto:
Why ?
For testing purposes I must to compile and run program with double or
float types.
sounds like a pretty good reason to me
Mark McIntyre ha scritto:
Why ?
For testing purposes I must to compile and run program with double or
float types.
"Most of them" seems perfectly reasonable to me.
CBFalconer said:You are wrong,
because that floating point value makes no assertion
about any exact value.
It only asserts that the real value is
between established limits. Any other assertion comes from other
facts, such as the overall program structure. If you don't realize
this you will make serious usage errors.
CBFalconer said:No. Try compiling and running this, and explain the results.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
double d1 = 2.0, d2 = 6.0, d3 = 3.0, da, db, dc;
da = d1/d3;
db = d1/d2;
dc = d3 * db;
printf("da = %f, db= %f dc= %f\n", da, db, dc);
printf("da == (2 * db) = %d\n", (da == (2 * db)));
printf("((dc - 1.0) == 1) = %d\n", (dc - 1.0) == 1);
return 0;
}
You can't. You can only see if two floats are very close to each other.
pseudocode:
if ((x-y) < SMALL_DIFFERENCE) return true;
*most of them*! I think you understating the case by amounts that
require hewbrew letters to express
Mark said:Um, having worked in the sector for fifteen years, I incline to
disagree. If you want to compare two floating-point values then you
should compare the absolute difference to some defined tolerance
criterion.
What is true is that often you don't actually want to compare two such
numbers at all, you just think you do. An example might be whether a
third of 101 lire was the same as a twentieth of 679 lire. It was.
Mark McIntyre said:Mark said:On 21/04/09 11:18, Alessio Ribeca wrote:
Thank you, How can I check for [floating-point] equality ?
You can't. You can only see if two floats are very close to each other.
This bit of nonsense has cropped up more than once in this
thread, so I'm very much afraid people may believe it. But it's
still nonsense!
I disagree.But strict equality *is* testable in C, if you want to do it,
and don't listen to anyone who tries to tell you otherwise.
And don't listen to anyone who tells you its safe to use == to compare
floats. Really Eric, I'm shocked. Is this you, or a sockpuppet.
Mark McIntyre said:No, you're incorrect there. The value is stored in a representation
which may, or may not, be capable of storing the value 1 exactly, but
the C standard doesn't require it to.
It so happens that if stored in binary the value can be stored
exactly, but other coding schemes are allowable by the C standard.
Actually, the boot is on the other foot: you'd need to find a quote
that required the FP implementation to store integers exactly as
otherwise the C standard imposes no requirements.
Mark McIntyre said:No, you're incorrect there. The value is stored in a representation
which may, or may not, be capable of storing the value 1 exactly, but
the C standard doesn't require it to.
Mark McIntyre said:6.4.4.2(3)
For decimal floating constants, and also for hexadecimal floating
constants when FLT_RADIX is not a power of 2, the result is either the
nearest representable value, or the larger or smaller representable
value immediately adjacent to the nearest representable value, chosen
in an implementation-defined manner.
Since the FLT_RADIX need not be a power of 2, it is implementation
defined whether a given value can be stored precisely or not.
Keith said:.... snip ...
A floating point value makes no assertion at all. It can be
interpreted in any of several ways, depending on the context. Your
assertion that a given value represents a range is no better founded
than an assertion that it represents one specific value.
Given:
double x = 1.0;
the value of x is exactly 1.0, no more, no less. What that value
*means*, whether it's just that specific value or any arbitrary value
in some specified range, depends on the application.
Richard said:CBFalconer said:
.... snip ...
Values don't make assertions. Values just *are*. How we interpret
them is our business, not theirs.
<snip - and yes, I did read the bit I snipped, and no, I don't think
it's even remotely relevant. In fact, I think it's gibberish.>
Mark said:Mark said:On 21/04/09 11:18, Alessio Ribeca wrote:
Thank you, How can I check for [floating-point] equality ?
You can't. You can only see if two floats are very close to each other.
This bit of nonsense has cropped up more than once in this
thread, so I'm very much afraid people may believe it. But it's
still nonsense!
I disagree.
The C language *is* capable of testing for equality of two
floating-point values, using the `==' operator. Applied to two
identical values it will produce the result 1 (true);
This a true but useless fact.
Its like stating that flipping a coin is
guaranteed to turn up heads. Sure, its likely to happen - but it might
take forever.
And in my view its dangerous to state this because a noob reading your
post might stop here and go away thinking that the == operator can be
usefully used on floats.
And don't listen to anyone who tells you its safe to use == to compare
floats. Really Eric, I'm shocked. Is this you, or a sockpuppet.
Eric said:[... in anger and in haste ...]
Because floating point values are not exact. They always simply
assert they mean a value between V (the value printed) - x_EPSILON
and V + x_EPSILON. Replace x by FLOAT or DOUBLE, depending on type
used. The EPSILON values are found in float.h.
Phil said:.... snip ...
No C variable or value makes any assertions about anything at all.
Are C integers exact?
Yes.
What's four divided by three?
*most of them*! I think you understating the case by amounts that
require hewbrew letters to express
user923005 said:.... snip ...
On the other hand, the values that are representible in floating
point tend to be useful in practice.
And a number system that could actually store all true floating
point values would be "rather bulky."
Richard said:.... snip ...
So what? If the exponent is 0, the value of FLT_RADIX is certainly
irrelevant for 0 and 1 at the very least, so those are two solid
portable examples of numbers that can be represented exactly by
floating point values.
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.