LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2007, 02:18 PM   #1
manosagn
LQ Newbie
 
Registered: Nov 2006
Location: Greece
Distribution: Ubuntu 6.06 LTS
Posts: 9

Rep: Reputation: 0
Comparison between doubles in c++


Hi!

I'm writing a program that uses extensively doubles.
Let's get to the point:

I need to modify a double in order for a logical expression (based on comparisons) to evaluate "true".

The idea behind the following code is:
"If a<b<c<d, I must modify d the least so that d-c >= c-b.
Similar for a: b-a >= c-b"
In theory I would say d = c + (c-b) and a = c - (c-b)

Suppose I set: (under the convention: o for outer and i for inner)
o_low = a
i_low = b
i_up = c
o_up = d
l_gap = b-a
u_gap = d-c
i_size = c-b

This must hold (evaluate "true"):
Code:
t3 = ( u_gap >= i_size );
If not, I must change o_low (a) and o_up (d), so that it holds.

Having considered this:
http://www.linuxquestions.org/questi...ble+comparison

I have decided to use a threshold for comparing numbers with zero:
Code:
#define EPS_FACTOR 0.5E-14
Initially I get:
Code:
i_low= 0.17000000000000001
o_low= 0
l_gap= 0.17000000000000001
i_size= 0.42999999999999994
l_gap - i_size= -0.2599999999999999
i_up= 0.59999999999999998
o_up= 0.678769816323468
u_gap= 0.078769816323468023
u_gap - i_size= -0.35123018367653192
Through the code I update o_low and o_up as following:
Code:
o_low = i_low - i_size - EPS_FACTOR;//this updates
o_up = i_up + i_size + EPS_FACTOR;//this updates
i_size = i_up - i_low;//re-evaluate
l_gap = i_low - o_low;//re-evaluate
u_gap = o_up - i_up;//re-evaluate
After updating the variables, I get:

Code:
i_low= 0.17000000000000001
o_low= -0.2599999999999999
l_gap= 0.42999999999999994
i_size= 0.42999999999999994
l_gap - i_size= 0
i_up= 0.59999999999999998
o_up= 1.0299999999999998
u_gap= 0.42999999999999983
u_gap - i_size= -1.1102230246251565e-16
u_gap and i_size are ALMOST identical, yet "mysteriously" u_gap is still
smaller!!!

How could I possibly update o_up so that u_gap becomes larger than i_size?
(Remember that I must update o_up by the SMALLEST POSSIBLE NUMBER )

rewriting t3 as:
Code:
t3 = ((u_gap > i_size)||(fabs(u_gap - i_size) <= EPS_FACTOR));
will tackle this problem, but accumulative use of the updates could
make the code do nonsense.

Thanks for just reading this long post!
 
Old 12-05-2007, 07:08 AM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Suggestion 1: There are several libraries available for exact numerical computation. If computational speed is not a major consideration, using exact computation would avoid your problem.

Suggesion 2: Look at the binary representation of the values being compared and develop an algorithm to solve your problem at that level.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
binaries and doubles manolakis Programming 2 10-22-2007 07:08 PM
C++ floats and doubles ArlexBee-871RBO Programming 0 03-01-2007 09:04 PM
Question About Floats and Doubles gomez6211 Programming 3 09-13-2006 07:47 PM
ATI Mobility Radeon 9000 doubles game speed ichi Linux - Software 1 07-31-2005 06:27 AM
STL vector of 3 doubles? true_atlantis Programming 2 04-22-2004 10:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration