[Gambas-user] gb3: incorrect solution to If...Then condition

Benoît Minisini gambas at ...1...
Sat Apr 28 00:01:10 CEST 2012


Le 27/04/2012 20:44, Jussi Lahtinen a écrit :
> If Sin(0.5) ^ 2 + Cos(0.5) ^ 2<>  1 Then
> Print "This shouldn't be seen, but it is."
> Endif
>
>
> ? format((Sin(0.5) ^ 2 + Cos(0.5) ^ 2),"#.########################")
> 1
>
> What is going on?
>
>
> Jussi
>

It's very strange actually.

There is optimization in the "^" operator. If the power is an integer 
between -4 and +4, standard multiplication and division are used instead 
of the pow() mathematical function.

In other words, Sin(0.5) ^ 2 = Sin(0.5) * Sin(0.5).

If you want to force the use of the pow() function, write 'Sin(0.5) ^ 2.0'.

And then comes the strangeness: Sin(0.5) * Sin(0.5) does not return the 
same value as Sin(0.5) ^ 2.0 ! And Sin(0.5) ^ 2.0 + Cos(0.5) ^ 2.0 does 
return 1.0. But not Sin(0.5)*Sin(0.5) + Cos(0.5)*Cos(0.5), which should 
be more accurate!

If someone has an explanation...

-- 
Benoît Minisini




More information about the User mailing list