Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ihlpf!nevin1 From: nevin1@ihlpf.ATT.COM (00704A-Liber) Newsgroups: comp.lang.c Subject: Re: Power operator? Message-ID: <3350@ihlpf.ATT.COM> Date: 13 Jan 88 01:28:08 GMT References: <47000029@uxe.cso.uiuc.edu> <10063@mimsy.UUCP> <646@l.cc.purdue.edu> <3312@ihlpf.ATT.COM> <651@l.cc.purdue.edu> Reply-To: nevin1@ihlpf.UUCP (00704A-Liber,N.) Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 66 In article <651@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) writes: >In article <3312@ihlpf.ATT.COM>, nevin1@ihlpf.ATT.COM (00704A-Liber) writes: .. In article <646@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) writes: .. .In article <10063@mimsy.UUCP>, chris@mimsy.UUCP (Chris Torek) writes: .. .. In article <47000029@uxe.cso.uiuc.edu> mcdonald@uxe.cso.uiuc.edu writes: > .. .Similar thinking would require one to write x = sub(y, z) instead of .. .x = y - z. This is a major design flaw in all HLLs which I know. .. .. In other words, are you saying that all functions of two variables which do not .. modify the variables should always be declared with infix notation?? I have a .. hard enough time remembering all the infix operators now (there are at least .. 19) and their order of precedence and associativity. :-) How would you define .. the order of precedence and associativity for these functions? > >There is a vast difference between requiring infix notation and permitting >infix notation. As far as precedence is concerned, who remembers the order >for most combinations? I would just as soon require parentheses for this >problem. What is the relative precedence of << (shift) and +? The various >people I asked came up with the _same_ answer. The C compiler on the Pyramid >came up with the other. Then the people you know do not know K&R C. Look in section 2.12 of K&R for precedence and order of associativity of operators; it is very well defined there. ('<<' and '..' are just below '+' and '-' (subtract, not minus) and both have left-to-right associativity.) Are you telling me that you *never* use the fact that multiplication has higher precedence than addition?? And without *any* definition of precedence, your compiler would have to flag expressions like 'a + b POWER c' (where 'POWER' is your power operator) as an error; it is ambiguous. Your addition would add ambiguity to a pretty unambiguous language (I KNOW I'll get flames for that last phrase :-)). > > .. .Another is that there are _several_ power functions; the one in the standard .. .math library is the slowest one, which must be used if the exponent is non- .. .integral. I said: .. Write a function (call it mypow()) to call the correct pow() function depending .. on its arguments; why should this be built in to the language if you can easily .. define it with the operators given? > >That C is more general than a number-crunching language is no reason for C to >do a bad job of number-crunching. We do not need any tradeoffs here. Your >argument about introducing a new function mypow() completely misunderstands the >problem. How would mypow() possibly know that the second argument is of type >integer, and that in that case it should proceed according to the type of the >first argument? Now you may say that I should also feed it the types of the >arguments. This is contrary to the whole idea of overloaded operators. If we >did not have this, instead of the 19 binary operations you mentioned above, we >would have more than 50 and have to remember all their names and precedences. Since you, the programmer, know if the second argument is an integer, you can call a special routine (call it ipow(), for example) to implement your algorithm. C is very loosely typed and it does not permit the programmer himself to overload operators. Your application is very easy to define in a language like C++; it allows overloading of operators, inlining functions, and calling different routines based on different types. It is not necessary to expand C to C++; just USE C++ instead of C (and get this discussion out of here :-)). -- _ __ NEVIN J. LIBER ..!ihnp4!ihlpf!nevin1 (312) 510-6194 ' ) ) "The secret compartment of my ring I fill / / _ , __o ____ with an Underdog super-energy pill." / (_