- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Testing for equal values in c
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2002 03:38 PM
тАО02-20-2002 03:38 PM
I read in a thread earlier today that it was bad to test two floating point values for equality. Can someone please explain this more fully? I have been working on a lot a Informix ESQL/C code that has many comparisons between double values.
TIA, Mary
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2002 03:59 PM
тАО02-20-2002 03:59 PM
SolutionI suppose that you are referring to this thread: http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xb87550011d20d6118ff40090279cd0f9,00.html
The answer is yes is it very bad for the same reason that 2 * 3 = 5.9999999999 on some calculators. The problem is that internally floating-point values are represented in base-2 logarithms of finite precision but you want to see the results in base 10. There is not an exact correspondence between these values.
To make matters worse, there is even a negative zero and a positive zero and these compare differently as well. In this case, the only difference between the two is the sign bit but they are different values.
I suppose an example is in order:
double a = 1.25, b = 1.25;
BAD:
if (a == b)
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}
This code will work sometimes and sometimes not depending upon the values compared especially when you really intended to test
1.24999999999 and 1.250000000001. You probably meant for these to evaluate equal but they will not. The fix is to do something like this:
#define SMALL_NUMBER 1.0e-6
if (fabs(a - b) <= SMALL_NUMBER)
{
printf("a equals b (more or less)\n");
}
else
{
printf("a not equal to b\n");
};
This stuff is pretty standard technique for dealing with floating-point values and I assure you that if you don't do this now you will do it later.
Food for thought, Clay
P. S. If you like, I can dig up one of my standard functions, nearlyequal(), which does just what its name implies.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2002 04:13 PM
тАО02-20-2002 04:13 PM
Re: Testing for equal values in c
I've attached a small int function nearlyequal() to help you.
Use if like this:
double a = 1.249999999, b = 1.25000000001;
if (nearlyequal(a,b))
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}
The function returns 1 if the values are equal (or nearly so) and 0 otherwise.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-20-2002 04:14 PM
тАО02-20-2002 04:14 PM
Re: Testing for equal values in c
Ooops ... I'm stupid, I missed the attachment.
I've attached a small int function nearlyequal() to help you.
Use if like this:
double a = 1.249999999, b = 1.25000000001;
if (nearlyequal(a,b))
{
printf("a equals b\n");
}
else
{
printf("a not equal to b\n");
}
The function returns 1 if the values are equal (or nearly so) and 0 otherwise.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-22-2002 02:08 AM
тАО02-22-2002 02:08 AM
Re: Testing for equal values in c
all floats or doubles have a fintie precision. If you are doing numerical calculations all calculations can introduce small errors since these calculation are done only with finite precision, too. Thus if you are testing two floats for equality these two floats can have different values although theoretically they are equal if all calculations are done without error. Therefore a so-called epsilon-test (see Clay's attachment) is used for testing in numerical calculations.
By the way you can find in many books about numerics techniques to deal with errors introduced by the finite precision calculations. I'm doing a lot numerical simulations and handling floats in a wrong way can cost a lot of time.
Bye,
-Thomas