I still find it fascinating that since we've moved from SANE to floating pointing number that we (computing professionals) have not come up with a way to adequately solve the problem of comparisons. Some years ago you, (RP) introduced a paper named "What Every Computer Scientist Should Know about Floating-Point." I don't know if it is still valid, I recall getting a few pages into the document and feeling my brain swell. Maybe it is still a good reference for all things floating point.
W.
-----Original Message-----
From: futurebasic@... [mailto:futurebasic@...] On Behalf Of Robert Purves
Sent: Thursday, February 24, 2011 1:05 AM
To: futurebasic@...
Subject: Re: [FB] comparing floating point numbers
Brian S wrote:
> Bernie wrote:
>> Wave wrote:
>>
>>> Will the encapsulating the floating point numbers in CFNumbers provide better comparison capability or will it just abstract the immediate problem away? I wonder what the overhead is. I don't know if this is still but I wonder if there is a lot of boxing and unboxing overhead with CFNumbers like there was (is?) with Java.
>>
>> '------ CFNumber ------
>> '----- NSNumber ------
> <snip of Bernie's nice demos>
>
> Notice how Core Foundation/Foundation has already solved the problem.
The OP's problem may have arisen from code like this:
long if ( lengthPieceA + lengthPieceB == lengthOfSteelBeam )
trim_ends_with_rusty_old_file()
xelse
immediately_engage_megawatt_plasma_arc()
endif
in which case a warning from the compiler is highly desirable.
Warning: clang:
comparing floating point with == or != is unsafe [-Wfloat-equal]
Comparing via the laborious CFNumberCreate/CFEqual/CFRelease, as in Bernie's demo, prevents the warning.
The possibly dangerous bug (which is inherent in floating point arithmetic), is alas still present.
In an engineering context, such as measuring steel beams, the amount of slop for floating-point 'equality' can be decided beforehand (depending on the units of length etc in use), and a simple test along these lines would be appropriate:
local fn FPValuesAreNearlyEqual( val1 as double, val2 as double ) as Boolean
'~'1
end fn = ( abs( val1 - val2 ) < 0.0001 )
Robert P.
--
To unsubscribe, send ANY message to: futurebasic-unsubscribe@...
To access the list archives, go to: http://freegroups.net/groups/futurebasic/