473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Bugs in the FAQ....

I reported this bug years ago but it's still not fixed.

On this page: http://c-faq.com/fp/fpequal.html

it says to use the following code:

if (fabs(a - b) <= epsilon * fabs(a))

What happens when a is zero and b is (eg.) epsilon/2?

In this case a and b should compare as equal, but they don't...

:-)
--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.
Jul 14 '08 #1
9 1201
fungus wrote:
I reported this bug years ago but it's still not fixed.

On this page: http://c-faq.com/fp/fpequal.html

it says to use the following code:

if (fabs(a - b) <= epsilon * fabs(a))

What happens when a is zero and b is (eg.) epsilon/2?

In this case a and b should compare as equal, but they don't...

:-)
--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.
If you read the next line it will say:

.... where epsilon is a value chosen to set the degree of ``closeness''
(and where you know that a will not be zero).

So a can't be zero
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jul 14 '08 #2
On Jul 14, 4:50*pm, jacob navia <ja...@nospam.comwrote:
>
If you read the next line it will say:

... where epsilon is a value chosen to set the degree of ``closeness''
(and where you know that a will not be zero).

So a can't be zero
Ah, Ok. It didn't used to say that....

So is that the "fix" then? :-)
--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.
Jul 14 '08 #3
fungus wrote:
On Jul 14, 4:50 pm, jacob navia <ja...@nospam.comwrote:
>If you read the next line it will say:

... where epsilon is a value chosen to set the degree of ``closeness''
(and where you know that a will not be zero).

So a can't be zero

Ah, Ok. It didn't used to say that....

So is that the "fix" then? :-)
--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.

Well, it says that in the link you gave us.

Obviously it would be better if the code made no such an assumption like

if( a == 0 || fabs(a - b) <= epsilon * fabs(a))

but all the faqs have bugs and this one is not an exception. If you
reread the text it looks like the sentence within the parenthesis was
hastily added after someone discovered that...
well you know :-)

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jul 14 '08 #4
fungus wrote:
) I reported this bug years ago but it's still not fixed.
)
) On this page: http://c-faq.com/fp/fpequal.html
)
) it says to use the following code:
)
) if (fabs(a - b) <= epsilon * fabs(a))
)
) What happens when a is zero and b is (eg.) epsilon/2?
)
) In this case a and b should compare as equal, but they don't...

Why should a and b compare as equal in that case ?
Consider the case where a = 0.001 and b = epsilon/2, for example.

However, a much more serious problem with the above code is that it
is not commutative. compare(a,b) is not always equal to compare(b,a).

To be commutative, shouldn't the code be something like:

if (fabs(a-b) <= epsilon * max(fabs(a), fabs(b)))

(Which, incidentally, solves the a=0 problem also)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jul 14 '08 #5
On Jul 14, 5:04*pm, jacob navia <ja...@nospam.comwrote:
>
Obviously it would be better if the code made no such an assumption
I changed it to:

if (((a == 0) && (b<epsilon)) || (fabs(a - b) <= epsilon * fabs(a)))
There's still a _tiny_ bug because:

(epsilon * fabs(a)) != (epsilon * fabs(b))

But it's a lot better....

well you know :-)
Yeah, I know...

--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.
Jul 14 '08 #6
On Jul 14, 5:13*pm, Willem <wil...@stack.nlwrote:
>
Why should a and b compare as equal in that case ?
Because "epsilon" is pretty close to zero...?
--
<\___/>
/ O O \
\_____/ FTB. Remove my socks for email address.

Jul 14 '08 #7
jacob navia <ja***@nospam.comwrites:
fungus wrote:
>On Jul 14, 4:50 pm, jacob navia <ja...@nospam.comwrote:
>>If you read the next line it will say:

... where epsilon is a value chosen to set the degree of ``closeness''
(and where you know that a will not be zero).

So a can't be zero
Ah, Ok. It didn't used to say that....
So is that the "fix" then? :-)

Well, it says that in the link you gave us.

Obviously it would be better if the code made no such an assumption like

if( a == 0 || fabs(a - b) <= epsilon * fabs(a))

but all the faqs have bugs and this one is not an exception. If you
reread the text it looks like the sentence within the parenthesis was
hastily added after someone discovered that...
[...]

Yes, but that's not the right fix. It assumes that a and b are
"equal" whenever a==0.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 14 '08 #8
fungus wrote:
On Jul 14, 5:04 pm, jacob navia <ja...@nospam.comwrote:
>Obviously it would be better if the code made no such an assumption

I changed it to:
[The "it" is part of one of the FAQ's suggestions on how to
compare floating-point numbers for approximate equality]
if (((a == 0) && (b<epsilon)) || (fabs(a - b) <= epsilon * fabs(a)))
There's still a _tiny_ bug because:
... because it says 0 and -1E30 are approximately equal?

--
Er*********@sun.com
Jul 14 '08 #9
On Jul 14, 7:47 am, fungus <openglMYSO...@artlum.comwrote:
I reported this bug years ago but it's still not fixed.

On this page:http://c-faq.com/fp/fpequal.html

it says to use the following code:

if (fabs(a - b) <= epsilon * fabs(a))

What happens when a is zero and b is (eg.) epsilon/2?

In this case a and b should compare as equal, but they don't...

:-)
It depends on whether you are trying to calculate relative or absolute
error.

This is one possible relative error comparison for floating point
types:

#include <float.h>
#include <math.h>

int double_compare (double d1, double d2)
{
if (d1 d2)
if ((d1 - d2) < fabs (d1 * DBL_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabs (d2 * DBL_EPSILON))
return 0;
else
return -1;
return 0;
}

int float_compare (float d1, float d2)
{
if (d1 d2)
if ((d1 - d2) < fabsf (d1 * FLT_EPSILON))
return 0;
else
return 1;
if (d1 < d2)
if ((d2 - d1) < fabsf (d2 * FLT_EPSILON))
return 0;
else
return -1;
return 0;
}

On the other hand, maybe you do not want relative error.
An example might be if you have a floating point function that is
given to a root search algorithm.
When the root has a y value of DBL_EPSILON you have found the zero and
you do not want to keep searching or refining. So the above method
would be incorrect for that application.
I don't think that there is a simple answer to the problem.

Jul 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: kartik | last post by:
there seems to be a serious problem with allowing numbers to grow in a nearly unbounded manner, as int/long unification does: it hides bugs. most of the time, i expect my numbers to be small. 2**31...
2
by: TheSteph | last post by:
Using : Windows 2000 Pro SP4 / VS.NET 2005 / .NET 2.0 / C# - All updates done. I have several bugs when I use the DataGridView : When scrolling (or after scrolling) the grid have these...
19
by: Alan Silver | last post by:
Hello, Having discovered what I believe to be two CSS bugs in IE7, I have submitted bug reports to MS. AFAIK, these don't get acted on until they receive votes form others to say they are worth...
15
by: Gary Peek | last post by:
Can anyone tell us the browsers/versions that exhibit errors when tables are nested too deeply? And how many levels of nesting produces errors? (not a tables vs CSS question)
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.