Re: math operators
- Posted by Elliott Sales de Andrade <quantum_analyst at hotmail.com> Sep 20, 2003
- 430 views
>From: Juergen Luethje <j.lue at gmx.de> >Subject: Re: math operators > >Jon wrote: > > >> Lucius Hilley wrote: > >> > >> POWER > >> I would love to see power(4, 5) become 4^5. > >> I know that we wouldn't be able to get read of power() > > > > YES! i prefer "^" but another alternative is "**" (like Ruby) > > >I never saw a program, that uses '++' as alternative for '*', or '--' as >alternative for '/'. I know, that there are some programs that use '**' >as alternative for '^', but I don't see any reason, why that should be >useful. > In C, ++ is equivalent to +=1 and -- is equivalent to -=1, in most cases. Pointers complicate things of course. Powers are repeated multiplication, so ** somewhat makes sense for this, but of course, multiplication is repeated addition, so someone might just think it is ++. I have seen ** in a few other languages. > >The following text explains, why I prefer 'power(x,y)' to 'x^y': > >Math operations of the same priority, such as 7+5+1, are normally >evaluated from left to right, if no parentheses are used. OK. > >And how about the power operator? What is the result of > 2^3^2 ? > >Mathematically, evaluation is right to left for the power operator! So > 2^3^2 = 2^(3^2) = 2^9 = 512. > I think the problem here is that it is hard to tell what you mean when the exponents are not in superscript. I think the way you see it, 2^3^2 is 2 with a superscript 3 to the upper right, and the 3 has a superscript 2 to the upper right of it. The "industry standard" sees it as (2^3)^2, ie, 2 with a superscript 3, all in brackets, and then with a superscript 2 for the brackets. 2^(3^2) translates to 2^9, which is 512, but (2^3)^2 is 2^(3 * 2), then 2^6, making 64. This is the major problem with the power symbol. It is too ambigous to tell which form was meant. Even C had this problem, and decided to go with the pow() function. (For those who thought ^ was power, it really is bitwise XOR.) >But there seems to be a kind of "industry standard", that says that such >expressions should be calculated from left to right. That means: > 2^3^2 = (2^3)^2 = 8^2 = 64 > This right-to-left thing really depends on how you think 2^3^2 should look on paper. Mathematics does say that expressions are evaluated left-to-right, no matter what. It is because you see it as 2^(3^2) that makes the difference. >For example all PowerBASIC versions that I know, and AFAIK also Visual >Basic calculates such expressions this way. >I have a pocket calculator that doesn't perform it's calculation until >the entire expression has been entered (very nice, BTW). >It returns 64 for 2^3^2, too (which is false, strictly mathematically >speaking). > My calculator, which does the same thing, evaluates it to 64, as well. >IMHO, this situation is very strange. >And Euphoria completly avoids this dilemma by using a 'power()' function, >instead of a power operator!!! I was very pleased, when I noticed that. > Yes, this situation is quite annoying. However, proper use of brackets, to clarify your meaning, will fix the problem. >So beside Euphorias obvious elegance, it has some "hidden elegance", >that cannot be seen at the first glance. > >