[Gambas-user] Ambiguous expression?

Les Hardy les at ...2337...
Wed Mar 10 20:04:39 CET 2010


Benoît Minisini wrote:
>> Alessandro Rinaldi ha scritto:
>>     
>>> I really think yes.
>>> Standard symbol for fractions is /, why should you use \?
>>>       
>> Backslash is "integer division", and should yeld an integer result.
>> Slash is "float" division, and yelds a float number (this is "Basic"
>> syntax; other languages use different methods).
>>
>> The expression "1\2*3" is not really ambiguous, it is only for Gambas
>> (don't ask me why); it should be seen as "take an integer number,
>> integer-divide it for another integer number, and multiply the result
>> for another integer number, giving an integer result".
>>
>> About integer division, there are other reasons apart from wanting an
>> *integer* division: for example is faster.
>>
>> Regards,
>> Doriano
>>
>>     
>
> I noticed that operator evaluation order differ between languages, even if 
> they agree on most parts.
>
> So I decided to raise an error when the compiler encounters different 
> operators having the same evaluation order weight, and when I thought that the 
> interpreter may not do what the user thinks.
>
> By compelling the user to add brackets, I make the source code less ambiguous 
> for another reader, so this is a good thing.
>
> But the test is not perfect, and I don't remember why I do not raise the error 
> for 1/2*3 and I raise it for 1\2*3.
>
> Maybe because that mixing "*" and "/" is usual in mathematics, but mixing an 
> integer division with a normal multiplication not, and I wanted to force the 
> use of brackets.
>
> Morever, that error is actually just a warning. I can suppress it, and Gambas 
> will just apply its operator evaluation order without telling anything. But I 
> don't think I will do it!
>
> Regards,
>
>   
You are correct to not suppress it. The warning is correct.
The actual value of the numbers is not important.
The expression is ambiguous without the brackets.
 5 \ (2 * 3) = 0
 (5 \ 2) * 3 = 6

This is also ambiguous
5/2*3

 5 / (2 * 3) = 0.833333333333
 (5 / 2) * 3 = 7.5

Regards
Les Hardy






More information about the User mailing list