For a problem
How many pair x, y of positive integers not greater than 100 satisfy the inequality
1/2 ≦ 1/x + 1/y ≦ 3/4,x≦y
I made a following program.
100 LET n=0 110 FOR x=1 TO 100 120 FOR y=x TO 100 125 LET z=1/x + 1/y 130 IF 1/2 <= z and z <= 3/4 THEN 140 LET n=n+1 150 PRINT x;y 155 END IF 160 NEXT y 170 NEXT x 180 PRINT n 190 END
This answers correctly, but if lines 125 and 130 are substituted by the following, the program misses (x,y)=(3,6).
125 130 IF 1/2<=1/x+1/y AND 1/x+1/y<= 3/4 THEN
In Full BASIC, if the precision of numerical variables is m digits, numerical expressions are evaluated in the precision of over m + 1 digits.
In Decimal BASIC, as the precision of numerical variables is 15 digits, numerical expressions are evaluated in the precision over 16 digits.
In Decimal BASIC, when x=3
and y=6
,
the calculation result of 1/x+1/y
is 0.499999999999999999999999999,
then, 1/2<=1/x+1/y
becomes false.
But when it is assigned to a numerical variable z, it is rounded to the precision of 15 digits and then z becomes 0.5, thus, 1/2 <= z
becomes true.
The situation mentioned above can be inspected easily by setting "More Places Displayed" be checked in Option - Precision menu, because, if the option has been set so, Decimal BASIC displays whole the digits of calculation result.
In addition, it is recommended that you should use the rational operation mode of Decimal BASIC when you want to handle with fractions accurately.