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

Kevin Fishburne kevinfishburne at ...1887...
Mon Apr 30 02:07:59 CEST 2012


On 04/27/2012 07:24 PM, Jussi Lahtinen wrote:
> At least the problem is unrelated to sin and cos...
>
> ? format(0.47942553860420300027 ^ 2 -
> 0.22984884706593014129,"#.######################")
> - .0000000000000012212453
>
> ? format(0.47942553860420300027 ^ 2.0 - 0.22984884706593014129
> ,"#.######################")
> - .0000000000000011379786
>
>
> Jussi
>
>
>
>
>
> 2012/4/28 Benoît Minisini<gambas at ...1...>
>
>> 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...

What would I do without you guys to humiliate me into wisdom? If I could 
beat floating point math with a stick I'd have enough kindling for a 
long winter. I used this code instead and the math is happy:

     Case Key["-"]
       ' Zoom in.
       Zoom.Adjustment = Zoom.Adjustment / 2
       If Zoom.Adjustment >= Zoom.Maximum Then Zoom.Adjustment = 
Zoom.Maximum
     Case Key["="]
       ' Zoom out.
       Zoom.Adjustment = Zoom.Adjustment * 2
       If Zoom.Adjustment <= Zoom.Minimum Then Zoom.Adjustment = 
Zoom.Minimum

Thanks.

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list