Bay Six Software Forum Index Bay Six Software
Beyond the Basics
 
 FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Experiment - FP comparisons

 
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language
View previous topic :: View next topic  
Author Message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 13th, 2013, 2:10am    Post subject: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Sep 13th, 2013, 12:42pm    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 13th, 2013, 3:17pm    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message Send e-mail
STPendl
Full Member


Joined: 20 Aug 2007
Posts: 161
Location: Austria

PostPosted: Sep 14th, 2013, 9:30am    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 14th, 2013, 3:51pm    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message Send e-mail
RichardRussell
Full Member


Joined: 28 Jan 2012
Posts: 57
Location: Downham Market, UK

PostPosted: Sep 23rd, 2013, 8:38am    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message Visit poster's website
Brent
Site Admin


Joined: 01 Jul 2005
Posts: 797

PostPosted: Sep 23rd, 2013, 4:11pm    Post subject: Re: Experiment - FP comparisons Reply with quote

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
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Bay Six Software Forum Index -> ViviFire language All times are GMT
Page 1 of 1
Jump to:  
Quick Reply
Username:
Message:
   Shortcut keys: Alt+Q to activate, Alt+P to preview, Alt+S to submit
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum



Lo-Fi Version
Powered by phpBB © 2001, 2005 phpBB Group