View previous topic :: View next topic |
Author |
Message |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Sep 13th, 2013, 2:10am Post subject: Experiment - FP comparisons |
|
|
I was just editing the docs for comparison operators when I was reminded that I wanted to provide a simple way to compare floating-point numbers with error tolerance. If you read the article, you will see what I came up with. Or if you don't want to read it, I'll repeat some here.
Basically, the comparison expression gains an optional following clause consisting of the "To" keyword and a number to specify the error tolerance of the comparison. Here's an example: Code: | If a = b To .0001 Then
' do something
End If |
Do you think this looks natural?
Do you think this overuses the To keyword? _________________ Brent |
|
Back to top |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Sep 13th, 2013, 12:42pm Post subject: Re: Experiment - FP comparisons |
|
|
This syntax doesn't look very clear to me.
Compared to "If ABS(a - b) <= .0001 Then" this is very unusual.
I don't think an overload of the equal sign is good either.
May be introduce a compare function?
CMP(a, b, "=", .0001) which returns True or False. _________________ Stefan
Any code I post can be freely used, just give credit. |
|
Back to top |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Sep 13th, 2013, 3:17pm Post subject: Re: Experiment - FP comparisons |
|
|
Stefan, it wouldn't be limited to just the = operator. It can be applied to any comparison, including a range comparison as well:
If a <= b <= c To .00001 Then ...
This would translate into LB as:
If (a < b Or Abs(a - b) <= .0001) And (B < c Or Abs(b - c) <= .0001) Then ...
Which one of these looks more natural to you?
Also, it came to me soon after my earlier post that this construct would need to be added to Select...Case.
Select Case test To tolerance
I can't add it to the "Case" clauses because they already use "To" for range comparisons. _________________ Brent |
|
Back to top |
|
 |
STPendl Full Member
Joined: 20 Aug 2007 Posts: 161 Location: Austria
|
Posted: Sep 14th, 2013, 9:30am Post subject: Re: Experiment - FP comparisons |
|
|
Sure the syntax is much neater and shorter, but as you say, the overload of the TO word is troublesome.
How about calling it TOL? _________________ Stefan
Any code I post can be freely used, just give credit. |
|
Back to top |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Sep 14th, 2013, 3:51pm Post subject: Re: Experiment - FP comparisons |
|
|
STPendl wrote: | How about calling it TOL? |
Good idea. And it seems appropriate because "tol" seems commonly accepted as an abbreviation of tolerance, which I was not sure about until I researched it.
Unless someone can come up with a more elegant solution, I'll be updating the grammar and docs to use this syntax. _________________ Brent |
|
Back to top |
|
 |
RichardRussell Full Member
Joined: 28 Jan 2012 Posts: 57 Location: Downham Market, UK
|
Posted: Sep 23rd, 2013, 8:38am Post subject: Re: Experiment - FP comparisons |
|
|
Brent wrote: | Do you think this looks natural?
Do you think this overuses the To keyword? |
Overloading the To keyword wouldn't bother me, because the language I'm most familiar with does that kind of thing all the time, but I wonder about the general usefulness of an 'arithmetic' rather than 'geometric' tolerance.
In applications when I've needed to do a 'compare with tolerance' I think I've just as frequently wanted a 'compare to within 1 part-per-million' as 'compare to within 0.0001'. It depends on the range of values you are expecting to be dealing with, and arguably a geometric tolerance is more in keeping with the nature of floating-point numbers. One can easily envisage a situation where a 0.0001 tolerance silently equates to 'precisely equal' because that value is smaller than a mantissa-LSB with the magnitude of numbers being compared.
I suppose one could work around this by comparing logarithms (although dealing with zero or negative numbers could be troublesome!), or by using a construction such as if a = b to a/1e6. To make that example work you would need to use the absolute value of the tolerance, otherwise it would be necessary to do if a = b to abs(a/1e6) to cope with negative values of a.
Richard. |
|
Back to top |
|
 |
Brent Site Admin
Joined: 01 Jul 2005 Posts: 790
|
Posted: Sep 23rd, 2013, 4:11pm Post subject: Re: Experiment - FP comparisons |
|
|
Richard, the syntax I suggested is simply to deal with everyday rounding errors. As such I find no need to muck up the construct by allowing complex expressions after the TO/TOL keyword. As it stands now, the grammar spec looks like:
Tolerance = "Tol" ( number | identifier )
I changed TO to TOL because it's clearer and avoids anyone coming from LB or other BASICs thinking that
IF a = 1 TO 1E-6 ...
might be some sort of looping construction. _________________ Brent |
|
Back to top |
|
 |
|