approxEqual() has fooled me for a long time...

Fawzi Mohamed fawzi at gmx.ch
Wed Oct 20 15:50:04 PDT 2010


On 20-ott-10, at 23:28, Don wrote:

> Fawzi Mohamed wrote:
>> On 20-ott-10, at 20:53, Don wrote:
>>> Andrei Alexandrescu wrote:
>>>> On 10/20/10 10:52 CDT, Don wrote:
>>>>> I don't think it's possible to have a sensible default for  
>>>>> absolute
>>>>> tolerance, because you never know what scale is important. You  
>>>>> can do a
>>>>> default for relative tolerance, because floating point numbers  
>>>>> work that
>>>>> way (eg, you can say they're equal if they differ in only the  
>>>>> last 4
>>>>> bits, or if half of the mantissa bits are equal).
>>>>>
>>>>> I would even think that the acceptable relative error is almost  
>>>>> always
>>>>> known at compile time, but the absolute error may not be.
>>>> I wonder if it could work to set either number, if zero, to the  
>>>> smallest normalized value. Then proceed with the feqrel  
>>>> algorithm. Would that work?
>>>> Andrei
>>>
>>> feqrel actually treats zero fairly. There are exactly as many  
>>> possible values almost equal to zero, as there are near any other  
>>> number.
>>> So in terms of the floating point number representation, the  
>>> behaviour is perfect.
>>>
>>> Thinking out loud here...
>>>
>>> I think that you use absolute error to deal with the difference  
>>> between the computer's representation, and the real world. You're  
>>> almost pretending that they are fixed point numbers.
>>> Pretty much any real-world data set has a characteristic  
>>> magnitude, and anything which is more than (say) 10^^50 times  
>>> smaller than the average is probably equivalent to zero.
>> The thing is two fold, from one thing, yes numbers 10^^50 smaller  
>> are not important, but the real problem is another, you will  
>> probably add and subtract numbers of magnitude x, on this operation  
>> the *absolute* error is x*epsilon.
>> Note that the error is relative to the magnitude of the operands,  
>> not of the result, it is really an absolute error.
>
> You have just lost precision.
> BTW -- I haven't yet worked out if we are disagreeing with each  
> other, or not.

eheh I think we both know the problems, and it is just the matter of  
the kind of tests we do more often.
feqrel is a very important primitive, and it should be available, that  
is what should have been used by Lars.
I happen to test often things where the result is a postion, an energy  
difference,... and in those cases I have a magnitude, and so  
implicitly also an absolute error.
In any case all this discussion was useful, as it made me improve my  
code by making the magnitude an explicit argument.

Fawzi



More information about the Digitalmars-d mailing list