* Bizarre output.
@ 2005-03-29 22:12 Srinath M
0 siblings, 0 replies; 9+ messages in thread
From: Srinath M @ 2005-03-29 22:12 UTC (permalink / raw)
To: gcc-help
Can someone tell me why the text, "I see it" is not displayed when this
program is run?
#include <stdio.h>
int main(void)
{
float a, b;
double d;
a = 0.;
b = 1.;
for (d = 0.0; d < 0.011; d = d + 0.001)
{
printf("d = %f\n",d);
if (d == 0.009)
{
printf ("I see it\n");
}
}
return 0;
}
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
2005-03-29 23:35 ` Antonio Coralles
2005-03-30 0:59 ` corey taylor
@ 2005-03-30 2:46 ` Eljay Love-Jensen
1 sibling, 0 replies; 9+ messages in thread
From: Eljay Love-Jensen @ 2005-03-30 2:46 UTC (permalink / raw)
To: Antonio Coralles, gcc-help
Hi Antonio,
>Hmm, maybe i understood something wrong, but didn't corey mean that his function double eps(double a, double b) calculates a magnitude based epsilon from fuzzyEpsilon and a ?
The definition of "close enough" can vary by the problem being considered. As such, there are several strategies (algorithms) one can employ to determine if a float or double is "close enough" to a given value.
My usual suggestion is "Use integers unless you can't."
>By the way, something completely differnet: What means HTH ?
HTH - hope that helps
--Eljay
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
2005-03-29 23:35 ` Antonio Coralles
@ 2005-03-30 0:59 ` corey taylor
2005-03-30 2:46 ` Eljay Love-Jensen
1 sibling, 0 replies; 9+ messages in thread
From: corey taylor @ 2005-03-30 0:59 UTC (permalink / raw)
To: Antonio Coralles; +Cc: gcc-help
Yes, I'm sorry if this was confusing, it was the only method I had and
works well.
This is the same as an evaluation within a fuzzyEpsilon error period,
except that error period changes according to the precision of the
numbers being evaluated.
corey
On Wed, 30 Mar 2005 01:33:55 +0200, Antonio Coralles
<noche.suapie@reflex.at> wrote:
> Eljay Love-Jensen wrote:
>
> > Hi Antonio,
> >
> >>What value of fuzzyEpsilon is appropriate for float / long double ? I
> > guess there is somkind of mathematical law in calculating it's value
> > from sizeof(value_type) ... Or is 0.000001 just an educated guess ?
> >
> > The value for fuzzyEpsilon is dependent on the magnitude of the
> > numbers involved, not on whether it is a double or float.
> >
> > HTH,
> > --Eljay
> >
> Hmm, maybe i understood something wrong, but didn't corey mean that his
> function
> double eps(double a, double b) calculates a magnitude based epsilon from
> fuzzyEpsilon and a ?
>
> By the way, something completely differnet: What means HTH ?
> antonio
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
2005-03-29 23:20 ` Antonio Coralles
2005-03-29 23:33 ` Eljay Love-Jensen
[not found] ` <fc.3b9aca00d11535353b9aca0024422d6b.42838a3@reflex.at>
@ 2005-03-30 0:18 ` corey taylor
2 siblings, 0 replies; 9+ messages in thread
From: corey taylor @ 2005-03-30 0:18 UTC (permalink / raw)
To: Antonio Coralles; +Cc: gcc-help
> > (void)b;
>
> If never seen this kind of cast before ... Is this statndard c/c++ or is
> it an gcc extension ? What is the effect of it ?
My apologies for the extra paramter. This function is part of a
library I help maintain, and we don't break old revisions much so it
was just left in apparantly.
(void)b;
This removes the unused parameter warning by using the parameter by
voiding the effect.
corey
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
[not found] ` <fc.3b9aca00d11535353b9aca0024422d6b.42838a3@reflex.at>
@ 2005-03-29 23:35 ` Antonio Coralles
2005-03-30 0:59 ` corey taylor
2005-03-30 2:46 ` Eljay Love-Jensen
0 siblings, 2 replies; 9+ messages in thread
From: Antonio Coralles @ 2005-03-29 23:35 UTC (permalink / raw)
To: gcc-help
Eljay Love-Jensen wrote:
> Hi Antonio,
>
>>What value of fuzzyEpsilon is appropriate for float / long double ? I
> guess there is somkind of mathematical law in calculating it's value
> from sizeof(value_type) ... Or is 0.000001 just an educated guess ?
>
> The value for fuzzyEpsilon is dependent on the magnitude of the
> numbers involved, not on whether it is a double or float.
>
> HTH,
> --Eljay
>
Hmm, maybe i understood something wrong, but didn't corey mean that his
function
double eps(double a, double b) calculates a magnitude based epsilon from
fuzzyEpsilon and a ?
By the way, something completely differnet: What means HTH ?
antonio
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
2005-03-29 23:20 ` Antonio Coralles
@ 2005-03-29 23:33 ` Eljay Love-Jensen
[not found] ` <fc.3b9aca00d11535353b9aca0024422d6b.42838a3@reflex.at>
2005-03-30 0:18 ` corey taylor
2 siblings, 0 replies; 9+ messages in thread
From: Eljay Love-Jensen @ 2005-03-29 23:33 UTC (permalink / raw)
To: Antonio Coralles, gcc-help
Hi Antonio,
>What value of fuzzyEpsilon is appropriate for float / long double ? I guess there is somkind of mathematical law in calculating it's value from sizeof(value_type) ... Or is 0.000001 just an educated guess ?
The value for fuzzyEpsilon is dependent on the magnitude of the numbers involved, not on whether it is a double or float.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
[not found] ` <fc.3b9aca00f5c969e73b9aca0024422d6b.428332a@reflex.at>
@ 2005-03-29 23:20 ` Antonio Coralles
2005-03-29 23:33 ` Eljay Love-Jensen
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Antonio Coralles @ 2005-03-29 23:20 UTC (permalink / raw)
To: gcc-help
corey taylor wrote:
> This is one method of fuzzy floating point equates using a
> magnitude-based epsilon.
>
> const double fuzzyEpsilon = 0.000001;
What value of fuzzyEpsilon is appropriate for float / long double ? I
guess there is somkind of mathematical law in calculating it's value
from sizeof(value_type) ... Or is 0.000001 just an educated guess ?
>
> /**
> Computes an appropriate epsilon for comparing a and b.
> */
> inline double eps(double a, double b) {
> // For a and b to be nearly equal, they must have nearly
> // the same magnitude. This means that we can ignore b
> // since it either has the same magnitude or the comparison
> // will fail anyway.
> (void)b;
If never seen this kind of cast before ... Is this statndard c/c++ or is
it an gcc extension ? What is the effect of it ?
> const double aa = abs(a) + 1;
> if (aa == inf()) {
> return fuzzyEpsilon;
> } else {
> return fuzzyEpsilon * aa;
> }
> }
>
> inline bool fuzzyEq(double a, double b) {
> return (a == b) || (abs(a - b) <= eps(a, b));
> }
>
> corey
>
antonio
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
2005-03-29 22:19 ` Antonio Coralles
@ 2005-03-29 22:37 ` corey taylor
[not found] ` <fc.3b9aca00f5c969e73b9aca0024422d6b.428332a@reflex.at>
1 sibling, 0 replies; 9+ messages in thread
From: corey taylor @ 2005-03-29 22:37 UTC (permalink / raw)
To: Srinath M, gcc-help
This is one method of fuzzy floating point equates using a
magnitude-based epsilon.
const double fuzzyEpsilon = 0.000001;
/**
Computes an appropriate epsilon for comparing a and b.
*/
inline double eps(double a, double b) {
// For a and b to be nearly equal, they must have nearly
// the same magnitude. This means that we can ignore b
// since it either has the same magnitude or the comparison
// will fail anyway.
(void)b;
const double aa = abs(a) + 1;
if (aa == inf()) {
return fuzzyEpsilon;
} else {
return fuzzyEpsilon * aa;
}
}
inline bool fuzzyEq(double a, double b) {
return (a == b) || (abs(a - b) <= eps(a, b));
}
corey
On Wed, 30 Mar 2005 00:12:58 +0200, Antonio Coralles
<noche.suapie@reflex.at> wrote:
> Srinath M wrote:
>
> > Can someone tell me why the text, "I see it" is not displayed when this
> > program is run?
> >
> > #include <stdio.h>
> >
> >
> > int main(void)
> > {
> > float a, b;
> >
> >
> > double d;
> >
> >
> > a = 0.;
> >
> >
> > b = 1.;
> >
> >
> > for (d = 0.0; d < 0.011; d = d + 0.001)
> > {
> > printf("d = %f\n",d);
> >
> >
> > if (d == 0.009)
> > {
> > printf ("I see it\n");
> > }
> > }
> > return 0;
> > }
> >
> Well, in my opinion you should normally not use "==" in conection with
> float or double, because floating-point arithmetic is not exact (not
> only with gcc). Besides 0.001 has not an exact representation as
> floating point number ... Try
> ...
> if(d > 0.008999 && d < 0.009001)
> printf(...);
> ...
> antonio
>
>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Bizarre output.
[not found] <fc.3b9aca006b2d42243b9aca0024422d6b.428315a@reflex.at>
@ 2005-03-29 22:19 ` Antonio Coralles
2005-03-29 22:37 ` corey taylor
[not found] ` <fc.3b9aca00f5c969e73b9aca0024422d6b.428332a@reflex.at>
0 siblings, 2 replies; 9+ messages in thread
From: Antonio Coralles @ 2005-03-29 22:19 UTC (permalink / raw)
To: Srinath M; +Cc: gcc-help
Srinath M wrote:
> Can someone tell me why the text, "I see it" is not displayed when this
> program is run?
>
> #include <stdio.h>
>
>
> int main(void)
> {
> float a, b;
>
>
> double d;
>
>
> a = 0.;
>
>
> b = 1.;
>
>
> for (d = 0.0; d < 0.011; d = d + 0.001)
> {
> printf("d = %f\n",d);
>
>
> if (d == 0.009)
> {
> printf ("I see it\n");
> }
> }
> return 0;
> }
>
Well, in my opinion you should normally not use "==" in conection with
float or double, because floating-point arithmetic is not exact (not
only with gcc). Besides 0.001 has not an exact representation as
floating point number ... Try
...
if(d > 0.008999 && d < 0.009001)
printf(...);
...
antonio
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2005-03-30 0:59 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-29 22:12 Bizarre output Srinath M
[not found] <fc.3b9aca006b2d42243b9aca0024422d6b.428315a@reflex.at>
2005-03-29 22:19 ` Antonio Coralles
2005-03-29 22:37 ` corey taylor
[not found] ` <fc.3b9aca00f5c969e73b9aca0024422d6b.428332a@reflex.at>
2005-03-29 23:20 ` Antonio Coralles
2005-03-29 23:33 ` Eljay Love-Jensen
[not found] ` <fc.3b9aca00d11535353b9aca0024422d6b.42838a3@reflex.at>
2005-03-29 23:35 ` Antonio Coralles
2005-03-30 0:59 ` corey taylor
2005-03-30 2:46 ` Eljay Love-Jensen
2005-03-30 0:18 ` corey taylor
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).