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!