Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Regarding Q. 14-5

Reply
Thread Tools

Regarding Q. 14-5

 
 
Eric Sosman
Guest
Posts: n/a
 
      12-03-2005
Mark McIntyre wrote:

> On Sat, 03 Dec 2005 15:09:32 -0500, in comp.lang.c , Eric Sosman
> <> wrote:
>
>
>> Malcolm raises an important problem, but does not show
>>how to solve it.

>
>
> Eric raises an important problem with any solution, but fails to show
> how to solve it
>
> The solution is indeed to compare to 1+epsilon, but to define epsilon
> suitably for your application. Its likely for instance that if you're
> doing calcs that involve rounding to 5dp, then a comparison to 4dp
> will succeed. If you're doing the sum I did earlier, then epsilon
> could be 1e-10 and that'd be successful.
>
> The precise method of determining epsilon is left as an exercise for
> the reader.


What I was trying to point out is that the supposed
"solution" is no solution at all. For example,

if ( f <= 1.0 )
angle = asin(f);

is not improved by replacing 1.0 with 1.0+epsilon ...

The solution -- to the degree that one exists -- is
to use care in calculating f in the first place, and to
understand the nature of the errors that occur along the
way and the manner in which they grow and decay. That
field of study is called Numerical Analysis, and is too
rich a topic to be treated at any depth in Usenet postings.

However, the fact that floating-point numbers are usually
(but not always!) approximations and that floating-point
calculations usually (but not always!) involve error should
not be taken as meaning that all floating-point comparisions
are "dangerous!"

I once read (but cannot now find) a description of a
progression of attitudes that may apply here:

1. Initially, people trust every digit of every number
a computer calculates.

2. Having learned that some (even many) digits can be
incorrect, the once-burned-twice-shy crowd overreacts
by never trusting any floating-point calculation.

3. With further experience and study, a more rational
(pun intentional) attitude of tolerance (ditto)
eventually develops. Floating-point arithmetic is
imperfect (as is int arithmetic) but like many other
tools is useful if handled with care.

It seems to me that someone whose alarm bells clang at `f<=1.0'
may not have completed the journey from stage 2 to 3. Malcolm
is probably at 2+epsilon or 3-delta, but afraid to calculate the
remaining distance

Recommended (strongly recommended) reading: "What Every
Computer Scientist Should Know About Floating-Point Arithmetic"
by David Goldberg, widely archived on the Web.

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
Mark McIntyre
Guest
Posts: n/a
 
      12-03-2005
On Sat, 03 Dec 2005 18:29:30 -0500, in comp.lang.c , Eric Sosman
<> wrote:

>
> What I was trying to point out is that the supposed
>"solution" is no solution at all.


I realise that, but my point is that you're wrong.

>For example,
>
> if ( f <= 1.0 )
> angle = asin(f);
>
>is not improved by replacing 1.0 with 1.0+epsilon ...


Of course it is. You just need to choose epsilon carefully, and decide
how to handle failures. The choice of both is highly dependent on how
you arrived at the FP value you want to examine, and how you want to
handle the problem cases.

#define EPS -1e-12

if ( f <= 1.0+EPS )
angle = asin(f);
else
{
puts("f ->1");
f = asin(1.0);
}

> Recommended (strongly recommended) reading: "What Every
>Computer Scientist Should Know About Floating-Point Arithmetic"
>by David Goldberg, widely archived on the Web.


Absolutely.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-03-2005
Mark McIntyre wrote:
>
> On Sat, 03 Dec 2005 15:09:32 -0500, in comp.lang.c , Eric Sosman
> <> wrote:
>
> > Malcolm raises an important problem, but does not show
> >how to solve it.

>
> Eric raises an important problem with any solution, but fails to show
> how to solve it
>
> The solution is indeed to compare to 1+epsilon, but to define epsilon
> suitably for your application.


It depends on what you're doing.

In the very few times that I have written code
which compares a double variable against a double constant
with either a relational operator or an equality operator,
it would be inappropriate to make use of DBL_EPSILON.
The (DBL_MAX >= x && x > 0) expression, is correct as is,
and so is the (0 != x) expression.

double sssqrt(double x)
{
if (DBL_MAX >= x && x > 0) {
const double a = x;
double b = x / 2 + 0.5;

do {
x = b;
b = (a / x + x) / 2;
} while (x > b);
} else {
if (0 != x) {
errno = EDOM;
x = HUGE_VAL;
}
}
return x;
}

In reviewing my toy math library, I've noticed that
I don't have any code which compares two double variables
with an equality operator.

All of my epsilon usage is kind of like this:

static double p_i(void)
{
unsigned n;
double p, a, b;

p = 0;
a = 3;
n = 1;
do {
a /= 9;
b = a / n;
a /= 9;
n += 2;
b -= a / n;
n += 2;
p += b;
} while (b > DBL_EPSILON / 4);
a = 2;
n = 1;
do {
a /= 4;
b = a / n;
a /= 4;
n += 2;
b -= a / n;
n += 2;
p += b;
} while (b > DBL_EPSILON / 2);
return 4 * p;
}

--
pete
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      12-04-2005
pete wrote:
> == is not a relational operator.


?


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-04-2005
August Karlstrom <> writes:

> pete wrote:
>> == is not a relational operator.

>
> ?


6.5.8 Relational operators
Syntax
1 relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression

[...]

6.5.9 Equality operators
Syntax
1 equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression != relational-expression

--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-04-2005
On Sat, 03 Dec 2005 23:59:57 GMT, in comp.lang.c , pete
<> wrote:

>Mark McIntyre wrote:
>
>> The solution is indeed to compare to 1+epsilon, but to define epsilon
>> suitably for your application.

>
>it would be inappropriate to make use of DBL_EPSILON.


Note carefully that I did not say DBL_EPSILON.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-04-2005
Mark McIntyre wrote:
>
> On Sat, 03 Dec 2005 23:59:57 GMT, in comp.lang.c , pete
> <> wrote:
>
> >Mark McIntyre wrote:
> >
> >> The solution is indeed to compare
> >> to 1+epsilon, but to define epsilon
> >> suitably for your application.

> >
> >it would be inappropriate to make use of DBL_EPSILON.

>
> Note carefully that I did not say DBL_EPSILON.


I would say that in cases
where a suitable definition of epsilon, is zero,
that the use of epsilon isn't required.

--
pete
 
Reply With Quote
 
August Karlstrom
Guest
Posts: n/a
 
      12-04-2005
Ben Pfaff wrote:
> August Karlstrom <> writes:
>
>
>>pete wrote:
>>
>>>== is not a relational operator.

>>
>>?

>
>
> 6.5.8 Relational operators
> Syntax
> 1 relational-expression:
> shift-expression
> relational-expression < shift-expression
> relational-expression > shift-expression
> relational-expression <= shift-expression
> relational-expression >= shift-expression
>
> [...]
>
> 6.5.9 Equality operators
> Syntax
> 1 equality-expression:
> relational-expression
> equality-expression == relational-expression
> equality-expression != relational-expression
>


OK, that's weird. Still `==' works as a relational operator in the
common (mathematical) sense.


August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      12-04-2005
August Karlstrom wrote:
> Ben Pfaff wrote:
>> August Karlstrom <> writes:
>>
>>
>>> pete wrote:
>>>
>>>> == is not a relational operator.
>>>
>>> ?

>>
>>
>> 6.5.8 Relational operators
>> Syntax
>> 1 relational-expression:
>> shift-expression
>> relational-expression < shift-expression
>> relational-expression > shift-expression
>> relational-expression <= shift-expression
>> relational-expression >= shift-expression
>>
>> [...]
>>
>> 6.5.9 Equality operators
>> Syntax
>> 1 equality-expression:
>> relational-expression
>> equality-expression == relational-expression
>> equality-expression != relational-expression
>>

>
> OK, that's weird. Still `==' works as a relational operator in the
> common (mathematical) sense.
>

Yes, it's a nice gotcha, isn't it? Like "byte", this is one of those terms C
appropriates in its own way and doesn't quite mean what you'd expect it to mean.

S.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-04-2005
August Karlstrom wrote:
>
> Ben Pfaff wrote:
> > August Karlstrom <> writes:
> >
> >
> >>pete wrote:
> >>
> >>>== is not a relational operator.
> >>
> >>?


> > 6.5.8 Relational operators


> > 6.5.9 Equality operators


> OK, that's weird. Still `==' works as a relational operator in the
> common (mathematical) sense.


I wrote what I wrote, plainly:

"== is an equality operator.
== is not a relational operator.

<= is a relational operator.
<= is not an equality operator."

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding viewing .asf files on my IP Lee Firefox 0 09-27-2005 03:36 PM
Regarding installation of Mozilla over Firebird and Thunderbird Delete Firefox 1 02-15-2005 08:29 PM
Question regarding newsgroups on Thunderbird Delete Firefox 5 02-12-2005 07:08 PM
Question Regarding Updating Firefox samx@north.net Firefox 2 07-09-2004 05:42 PM
help regarding msn and hotmail in mail vc Firefox 6 04-03-2004 08:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57