[Gambas-user] numerical error in Gambas??

Benoit Minisini gambas at ...1...
Thu Apr 1 18:32:07 CEST 2004


On Thursday 01 April 2004 00:08, Jesper Holm wrote:
> Hi Benoit,
>
> I am a little uneasy reporting this, but it seems that the IF..THEN
> statement is testing the "displayed" value and not the calculated value.
>
> PUBLIC SUB Main()
> DIM a AS Float
>   FOR a = 1.68 TO 1.70 STEP 0.001
>     IF (a * (1/a)) = 1 THEN
>       PRINT a, (a * (1/a)),"OK"
>     ELSE
>       PRINT a, (a * (1/a)),"not OK"
>     END IF
>   NEXT
> END
>
> Does prints some "not OK" values.

You are facing problems with floating point numbers common to every program. 
Never compare two floating point numbers, because even if Gambas displays 
('will' when the previous bug is fixed) 1, the number could be not exactly 
equal to 1. The difference is too small to be displayed.

>
> Do you know which parts of Gambas is affected by this bug? I have several
> of that kind of tests in my program.

Every floating point computed by your computer in every program is affected. 
The solution is testing the difference between the two numbers this way:

IF Abs((a * (1 / a)) - 1) < 1E-10 THEN
  PRINT "Almost equal"
ELSE
  PRINT "Really different"
ENDIF

>
> Also the SELECT..CASE and DO..LOOP structures are affected. (but I guess
> those are derived from the IF..THEN)
>
> Hope it can help narrowing down the problem.
>
> Meanwhile I'll just have to use a bunch of ROUND()'s here and there :-)

Yes, or use Round(): IF Round((a * (1 / a)) - 1, -10) = 0 THEN ...

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...




More information about the User mailing list