PureBytes Links
Trading Reference Links
|
> Value3 = .000001 for example, then use:
> if AbsValue (Value1 - Value2) < Value3 Then Value1 = 0;
>
> This allows numbers to be "equal" within some error that will
> always be there.
ztrader beat me to it. Not sure who the Gary was who sent Mats the
example, but it wasn't me. :-)
The problem lies in the differences between decimal and binary
numbers. You can have two numbers, computed from different
calculations, that appear equal in decimal (especially since TS
apparently chops off a few digits, maybe?), but they are not EXACTLY
EQUAL when you compare the binary values. Unfortunately the "="
operator is an EXACTLY EQUAL test. You can use ztrader's solution to
get around this.
I actually used a language back in the 70's where "=" was a "pretty
much equal" operator, and it avoided problems like this. That's the
only place I've ever seen a "fuzzy" equal test.
I *think* double-precision numbers, in Intel/AMD chips at least,
actually use extra bits in the mantissa calculation, then chop off
the extra bits at the end of the calculation. This can avoid or at
least reduce these "not quite equal" problems. Not sure about single
precision, but apparently it doesn't.
In any case, it's easy to see why the test failed. Print out the
difference between Mats' numbers, and you'll see they're not equal:
Value1 = 10.387 - 10.280;
Value2 = 10.280 - 10.173;
print(value1-value2:3:15);
That prints 0.000000953674000 on my TS4.
Gary
|