[futurebasic] divide 3 ways

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : March 2008 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Mon, 31 Mar 2008 14:59:00 +1300
FB is uniquely aberrant among programming languages in having three  
symbols '/'  '\'  and '\' for the division operator.
In nearly all circumstances the symbols appear to be synonyms. You get  
the same result from all three. It doesn't seem to matter which you  
pick.
'----------------
dim as double y1, y2, y3
y1 = 1/3 : y2 = 1 : y3 = 1\3
print y1, y2, y3
y1 = 2/3 + 2/3 : y2 = 2 + 2 : y3 = 2\3 + 2\3
print y1, y2, y3

dim as long x1, x2, x3
x1 = 1/3 : x2 = 1 : x3 = 1\3
print x1, x2, x3
stop
'----------------


In rare circumstances that I take care to avoid in my own code the  
division symbol *does* matter. This example prints a 1 and two 0s, not  
necessarily in that order.
'----------------
dim as long foo, bar, var
foo = 2/3 + 2/3 : bar = 2 + 2 : var = 2\3 + 2\3
print foo, bar, var
stop
'----------------

Suppose that you have code like that above, you Really Truly™ want the  
result of the addition to be 1, and you want others running your  
source to get 1 also. How can you ensure that? Should you use foo, bar  
or var?

Obviously var is no good. The '\' operator means that the division is  
done on the integer unit (see Appendix) so that both terms and their  
sum are 0.

The choice lies between foo and bar. A ridiculous fact is that you  
can't determine the values of foo or bar from inspecting the code. The  
value depends also on a checkbox in your preference settings:
   (FB)     [x] Use "\" for floating-point division
(FBtoC)    [x] \ forces floating division

I regard the existence of this checkbox as Insanely Stupid, and intend  
to remove it from FBtoC. †   The effect of the proposed change will be  
that '/' and '\' become synonyms, as they are now with FBtoC's  
default settings. In the second example above, foo will always be 0  
and bar always 1.

Most FB projects will be completely unaffected by the change. FBtoC,  
to take one moderately complicated example, runs identically when  
built with the checkbox ON and OFF.

† unless dissuaded by reasoned advocacy or ignorant tirades

Robert P.

Appendix
Divisions are done on either the CPU's integer unit or its floating- 
point unit
1/3     ---integer-unit---> 0
7/4     ---integer-unit---> 1
1.0/3.0 --------fpu-------> 0.3333333...
7.0/4.0 --------fpu-------> 1.75