Vladislav Boyko #: It seems that from a mathematical point of view this is not true.
But it seems that the expressions are not equivalent only when the difference between a and b is strictly equal to x.
Below is an analysis of the combinations from my previous post.
a 0.00810000, b 0.00510000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00840000, b 0.00540000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00870000, b 0.00570000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00900000, b 0.00600000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00930000, b 0.00630000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00960000, b 0.00660000, x 0.00300000, diff 0.00300000, diff == x true
a 0.00990000, b 0.00690000, x 0.00300000, diff 0.00300000, diff == x true
a 0.01020000, b 0.00720000, x 0.00300000, diff 0.00300000, diff == x true
a 0.01050000, b 0.00750000, x 0.00300000, diff 0.00300000, diff == x true
a 0.01080000, b 0.00780000, x 0.00300000, diff 0.00300000, diff == x true
---
a 0.00099000, b 0.00009000, x 0.00090000, diff 0.00090000, diff == x true
a 0.00108000, b 0.00018000, x 0.00090000, diff 0.00090000, diff == x true
a 0.00198000, b 0.00108000, x 0.00090000, diff 0.00090000, diff == x true
---
a 0.00027000, b 0.00000000, x 0.00027000, diff 0.00027000, diff == x true
a 0.00029700, b 0.00002700, x 0.00027000, diff 0.00027000, diff == x true
a 0.00032400, b 0.00005400, x 0.00027000, diff 0.00027000, diff == x true
a 0.00054000, b 0.00027000, x 0.00027000, diff 0.00027000, diff == x true
a 0.00056700, b 0.00029700, x 0.00027000, diff 0.00027000, diff == x true
a 0.00059400, b 0.00032400, x 0.00027000, diff 0.00027000, diff == x true
a 0.00062100, b 0.00035100, x 0.00027000, diff 0.00027000, diff == x true
a 0.00064800, b 0.00037800, x 0.00027000, diff 0.00027000, diff == x true
a 0.00067500, b 0.00040500, x 0.00027000, diff 0.00027000, diff == x true
a 0.00070200, b 0.00043200, x 0.00027000, diff 0.00027000, diff == x true
---
x = 0.00008100: No such values found in the given range.
---
a 0.00003888, b 0.00001458, x 0.00002430, diff 0.00002430, diff == x true
a 0.00004131, b 0.00001701, x 0.00002430, diff 0.00002430, diff == x true
---
Combinations analyzed: 1847338041
#define _lessOrEqualWay1(a, b) ((a) - (b) < x)
#define _lessOrEqualWay2(a, b) ((b) - (a) > -x)
#define _greater(a, b) ((a) - (b) > x)
#define _notGreater(a, b) (!_greater(a, b))
#define _toStr(a) (a) ? "true" : "false"class CCombination
{
public:
staticlong analyzed;
private:
constdouble m_a;
constdouble m_b;
constdouble m_x;
constdouble m_diff;
public:
CCombination(double a_a, double a_b, double a_x);
};
long CCombination::analyzed = 0;
CCombination::CCombination(double a_a,double a_b,double a_x)
: m_a(a_a),
m_b(a_b),
m_x(a_x),
m_diff(MathAbs(m_a - m_b))
{
PrintFormat("a %.8f, b %.8f, x %.8f, diff %.8f, diff == x %s", m_a, m_b, m_x, m_diff, _toStr(MathAbs(m_diff - m_x) < DBL_EPSILON));
}
voidOnStart()
{
CCombination* comb[];
double X = 0.01;
for(int i = 0; i < 5; i++)
{
X *= 0.3;
findValues(X, comb);
Print("---");
}
Print("Combinations analyzed: ", CCombination::analyzed);
for(int i = ArraySize(comb) - 1; i >= 0; i--)
delete comb[i];
}
void findValues(double x, CCombination *&comb[])
{
int found = 0;
double step = x / 10.0;
for (double a = 0.0; a <= 0.1; a += step)
for (double b = 0.0; b <= 0.1; b += step)
{
CCombination::analyzed++;
bool lessOrEqualWay1 = _lessOrEqualWay1(a, b);
bool lessOrEqualWay2 = _lessOrEqualWay2(a, b);
bool notGreater = _notGreater(a, b);
if (lessOrEqualWay1 != lessOrEqualWay2 || lessOrEqualWay2 != notGreater)
{
int size = ArraySize(comb);
ArrayResize(comb, size + 1, 50);
comb[size] = new CCombination(a, b, x);
found++;
if(found > 9)
return;
}
}
if(found < 1)
PrintFormat("x = %.8f: No such values found in the given range.", x);
}
Another possible solution could be to do the following:
Mask out the exponent and compare these, if they are equal, then mask out the fraction, set the LSB to zero on both and compare these. If they equal as well, the numbers are representing the same value.
Don't forget the sign bit as well.
I am not 100% sure it will work as intended, as I have not tested this.
Another possible solution could be to do the following:
Mask out the exponent and compare these, if they are equal, then mask out the fraction, set the LSB to zero on both and compare these. If they equal as well, the numbers are representing the same value.
Don't forget the sign bit as well.
I am not 100% sure it will work as intended, as I have not tested this.
It seems that from a mathematical point of view this is not true.
But it seems that the expressions are not equivalent only when the difference between a and b is strictly equal to x.
Below is an analysis of the combinations from my previous post.
But it seems that the expressions are not equivalent only when the difference between a and b is strictly equal to x.
Below is an analysis of the combinations from my previous post.
That's what happens when you forget your school math and you can't get a proof using math😄
Only now I saw that it would be more correct to replace the highlighted one with this:
X /= 3.0;I originally wanted to reduce it by 3 times, but I made a typo.
However, after replacing that line, the expressions are still not equivalent only when diff == x
Only now I saw that it would be more correct to replace the highlighted one with this:
I originally wanted to reduce it by 3 times, but I made a typo.
However, after replacing that line, the expressions are still not equivalent only when diff == x
This is interesting, thank you