473,381 Members | 1,508 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,381 software developers and data experts.

Efficient double comparison


Hello all,

I would like to solicit suggestions on how to most efficiently accomplish
something. I have profiled the project I'm working on and have found that
calls to fabs() are taking a very substantial amount of time. fabs() is
being used in the following manner:

double a, b;

a = get_a_value();
b = get_another_value();

if (fabs(a - b) > 1E-9)
// Do "not equal" stuff
else
// Do "equal" stuff

So, we're simply comparing two doubles for equality and making allowances
for inexact numeric representation.

Of course, we could save time by reducing the number of comparisons. But,
unfortunately, we do need all of the comparisons we're making - none are
redundant or unused. So... Can anybody out there suggest a more efficient
way of accomplishing these comparisons?

Thanks,
Dave
Jul 22 '05 #1
2 15148
"Dave" <be***********@yahoo.com> wrote

Hello all,

I would like to solicit suggestions on how to most efficiently accomplish
something. I have profiled the project I'm working on and have found that
calls to fabs() are taking a very substantial amount of time. fabs() is
being used in the following manner:

double a, b;

a = get_a_value();
b = get_another_value();

if (fabs(a - b) > 1E-9)
// Do "not equal" stuff
else
// Do "equal" stuff

So, we're simply comparing two doubles for equality and making allowances
for inexact numeric representation.

Of course, we could save time by reducing the number of comparisons. But,
unfortunately, we do need all of the comparisons we're making - none are
redundant or unused. So... Can anybody out there suggest a more efficient
way of accomplishing these comparisons?


You can always do something like this:

inline bool simpleFuzzyEq(const double & v1, const double & v2, const double &
epsilon)
{
const double diff = v1 - v2;

return diff < epsilon && diff > -epsilon;
}

The extra comparison is far less expensive than a call to fabs() and most
compilers will inline this to something extremely tight.

As a side note, a fuzzy equality test with an absolute epsilon has an extremely
limited scope and doesn't behave intuitively if the magnitudes of a comparand is
very large.

Claudio Puviani
Jul 22 '05 #2
Hi,

inline bool simpleFuzzyEq(const double & v1, const double & v2, const double
&
epsilon)
{
const double diff = v1 - v2;

return diff < epsilon && diff > -epsilon;
}
The extra comparison is far less expensive than a call to fabs() and most
compilers will inline this to something extremely tight.
This is something I absolutely cannot agree about, at least for Intel 80x86
architecture. FABS operation done in 80x86 FPU takes exactly 1 cycle, and
also pairs with the next operation (if they're not dependent). Practically -
it is free. Comparison between 2 floating point numbers is a complex thing
on the other hand. It can take as much as 4 cycles, plus extra instructions
to copy the result from FPU registers to CPU registers (to actually do the
conditional jump based on comparison result). Not to say that in the code
above there is an extra unary negation op, which has usually the same
complexity as FABS alone. And an extra '&&' operation...

I don't know other CPU architectures, but I think it's the same way round.

On 80x86, if precision up to 15th digit is not very important, I'd suggest
switching to float data and using _control87() to force FPU to work in
24-bit precision mode. _control87() is non-standard, but present in most
80x86 compilers libraries:

_control87(_PC_24, MCW_PC); // reduce precision
// here put plenty of intensive floating point operations. They'll work much
faster

_control87(_PC_53, MCW_PC); // restore precision

Best regards,
Marcin

Użytkownik "Claudio Puviani" <pu*****@hotmail.com> napisał w wiadomości
news:E_**********************@news4.srv.hcvlny.cv. net...
"Dave" <be***********@yahoo.com> wrote
Hello all,

I would like to solicit suggestions on how to most efficiently accomplish
something. I have profiled the project I'm working on and have found that
calls to fabs() are taking a very substantial amount of time. fabs() is
being used in the following manner:

double a, b;

a = get_a_value();
b = get_another_value();

if (fabs(a - b) > 1E-9)
// Do "not equal" stuff
else
// Do "equal" stuff

So, we're simply comparing two doubles for equality and making allowances
for inexact numeric representation.

Of course, we could save time by reducing the number of comparisons. But,
unfortunately, we do need all of the comparisons we're making - none are
redundant or unused. So... Can anybody out there suggest a more efficient
way of accomplishing these comparisons?


You can always do something like this:

inline bool simpleFuzzyEq(const double & v1, const double & v2, const double
&
epsilon)
{
const double diff = v1 - v2;

return diff < epsilon && diff > -epsilon;
}

The extra comparison is far less expensive than a call to fabs() and most
compilers will inline this to something extremely tight.

As a side note, a fuzzy equality test with an absolute epsilon has an
extremely
limited scope and doesn't behave intuitively if the magnitudes of a
comparand is
very large.

Claudio Puviani

Jul 22 '05 #3

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

Similar topics

6
by: Barry | last post by:
The following code compiled using MSVC++6.0 - double di=0.0; for(;di<=512.0;) { string.Format("%li",int(di)); pDC-> TextOut( left +int(di), 100,string); di += 51.2; }
10
by: Mr Doppino | last post by:
Hi people, I would like to submit a problem about double comparison. Here's the code: struct Cross_point { int cross_id; int x, y; int street_oriz, street_vert; }; ...
8
by: brian.digipimp | last post by:
I turned this in for my programming fundamentals class for our second exam. I am a c++ newb, this is my first class I've taken. I got a good grade on this project I'm just wondering if there is a...
6
by: Jesper, DK. | last post by:
A little preamble A year ago I had a very odd bug in my program, in an if clause it picked and executed the else statement even though the if clause should evaluate to true. Normally you know that...
44
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b,...
1
by: Jay Hamilton | last post by:
Hello, I am running into an invalid address alignment error on an HPUX box when I attempt to lookup a value in a STL map. The argument being passed in is a double, which is accessed from a...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file i/o seems to be the bottleneck. This is my code...
18
by: brekehan | last post by:
I ran across some code that is initializing a double: double x = 1e-5; Does the standard dictate the compiler to interpret that as 0.00001? or is the above representation error prone? ,...
74
by: copx | last post by:
In "Learning Standard C++ as a New Language" Bjarne Stroustrup claims that properly written C++ outperforms C code. I will just copy his first example here, which is supposed to demonstrate how C++...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.