Thread overview | |||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 07, 2009 Exponential operator | ||||
---|---|---|---|---|
| ||||
In the 'proposed syntax change' thread, Don mentioned that an exponentiation operator is sorely missing from D. I couldn't agree more. Daniel Keep has proposed the syntax a*^b while my suggestion was a^^b Neither of the natural candidates, a^b and a**b, are an option, as they are, respectively, already taken and ambiguous. "Why do we need this?" you say. "Isn't pow(a,b) good enough?" And yes, pow(a,b) is just as good as mul(a,b) or div(a,b), but we don't use those, do we? Exponentiation is a very common mathematical operation that deserves its own symbol. Besides, bearophile has pointed out several optimisations that the compiler can/must perform on exponential expressions. He also proposed that the overload be called opPower. What do you think? -Lars |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | On Fri, Aug 7, 2009 at 3:50 AM, Lars T. Kyllingstad<public@kyllingen.nospamnet> wrote: > In the 'proposed syntax change' thread, Don mentioned that an exponentiation operator is sorely missing from D. I couldn't agree more. > > Daniel Keep has proposed the syntax > > a*^b > > while my suggestion was > > a^^b > > Neither of the natural candidates, a^b and a**b, are an option, as they are, respectively, already taken and ambiguous. > > "Why do we need this?" you say. "Isn't pow(a,b) good enough?" And yes, > pow(a,b) is just as good as mul(a,b) or div(a,b), but we don't use those, do > we? Exponentiation is a very common mathematical operation that deserves its > own symbol. Besides, bearophile has pointed out several optimisations that > the compiler can/must perform on exponential expressions. > > He also proposed that the overload be called opPower. > > What do you think? I'm all for it. But if we can't get that, then it might be nice to have at least squaring and cubing template functions in std.math. Squaring numbers is so common it deserves a direct way. It always annoys me when I have to write float x = (some expression); float x2 = x*x; When I'd like to be able to just write (some expression)^^2. sqr(some expression) would be ok, too. It's odd that sqrt and cbrt exist in the std C math library but not their inverses. I found this list of languages with an exponentiation operator: # x ↑ y: Algol, Commodore BASIC # x ^ y: BASIC, J, Matlab, R, Microsoft Excel, TeX (and its derivatives), Haskell (for integer exponents), and most computer algebra systems # x ** y: Ada, Bash, Fortran, FoxPro, Perl, Python, Ruby, SAS, ABAP, Haskell (for floating-point exponents), Turing # x * y: APL --bb |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | Lars T. Kyllingstad: > He also proposed that the overload be called opPower. I want to add to two small things to that post of mine: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=95123 The name opPow() may be good enough instead of opPower(). And A^^3 may be faster than A*A*A when A isn't a simple number, so always replacing the power with mults may be bad. On the other hand if double^^2 is compiled as pow(double,2) then I'm not going to use ^^ in most of my code. Bye, bearophile |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | On Fri, 07 Aug 2009 12:50:25 +0200, Lars T. Kyllingstad wrote: > In the 'proposed syntax change' thread, Don mentioned that an exponentiation operator is sorely missing from D. I couldn't agree more. [..] > Neither of the natural candidates, a^b and a**b, are an option, as they are, respectively, already taken and ambiguous. [..] What is ** used for? |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Moritz Warning | On Fri, Aug 7, 2009 at 6:02 AM, Moritz Warning<moritzwarning@web.de> wrote:
> On Fri, 07 Aug 2009 12:50:25 +0200, Lars T. Kyllingstad wrote:
>
>> In the 'proposed syntax change' thread, Don mentioned that an exponentiation operator is sorely missing from D. I couldn't agree more.
> [..]
>> Neither of the natural candidates, a^b and a**b, are an option, as they are, respectively, already taken and ambiguous.
> [..]
>
> What is ** used for?
Multiplying by a dereferenced pointer.
--bb
|
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter wrote: > On Fri, Aug 7, 2009 at 3:50 AM, Lars T. > Kyllingstad<public@kyllingen.nospamnet> wrote: >> In the 'proposed syntax change' thread, Don mentioned that an exponentiation >> operator is sorely missing from D. I couldn't agree more. >> >> Daniel Keep has proposed the syntax >> >> a*^b >> >> while my suggestion was >> >> a^^b >> >> Neither of the natural candidates, a^b and a**b, are an option, as they are, >> respectively, already taken and ambiguous. >> >> "Why do we need this?" you say. "Isn't pow(a,b) good enough?" And yes, >> pow(a,b) is just as good as mul(a,b) or div(a,b), but we don't use those, do >> we? Exponentiation is a very common mathematical operation that deserves its >> own symbol. Besides, bearophile has pointed out several optimisations that >> the compiler can/must perform on exponential expressions. >> >> He also proposed that the overload be called opPower. >> >> What do you think? > > I'm all for it. > But if we can't get that, then it might be nice to have at least > squaring and cubing template functions in std.math. Squaring numbers > is so common it deserves a direct way. It always annoys me when I > have to write > float x = (some expression); > float x2 = x*x; > When I'd like to be able to just write (some expression)^^2. > sqr(some expression) would be ok, too. It's odd that sqrt and cbrt > exist in the std C math library but not their inverses. Yes, it's powers of 2 and 3 that are 90% of the use cases. Squaring is a really common operation (more common than xor). An optimising compiler always needs to recognize it. > I found this list of languages with an exponentiation operator: > # x ↑ y: Algol, Commodore BASIC > # x ^ y: BASIC, J, Matlab, R, Microsoft Excel, TeX (and its > derivatives), Haskell (for integer exponents), and most computer > algebra systems > # x ** y: Ada, Bash, Fortran, FoxPro, Perl, Python, Ruby, SAS, ABAP, > Haskell (for floating-point exponents), Turing > # x * y: APL > > --bb I personally don't understand why anyone would like ** as an exponentiation operator. The only thing in its favour is that that's what Fortran did. And Fortran only used it because it had almost no characters to choose from. ↑ is the best (yup, I had a C64). ^ is the next best, but unfortunately C grabbed it for xor. I think ^^ is the best available option. Aside from the fact that x**y is ambiguous (eg, x***y could be x ** (*y) or x * (*(*y)) ), I just think ^^ looks better: assert( 3**3 + 4**3 + 5**3 == 6**3 ); assert( 3^^3 + 4^^3 + 5^^3 == 6^^3 ); Found an old discussion, pragma proposed ^^ and opPower: http://www.digitalmars.com/d/archives/digitalmars/D/18742.html#N18742 The fact that exactly the same proposal came up again is encouraging -- it's moderately intuitive. For overloading, by analogy to opAdd(), opSub(), opMul(), opDiv() it should probably be opPow(). |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | "bearophile" <bearophileHUGS@lycos.com> wrote in message news:h5h3uf$23sg$1@digitalmars.com... > Lars T. Kyllingstad: >> He also proposed that the overload be called opPower. > > I want to add to two small things to that post of mine: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=95123 > > The name opPow() may be good enough instead of opPower(). > > And A^^3 may be faster than A*A*A when A isn't a simple number, so always > replacing the > power with mults may be bad. It wont be on x86. Multiplication has a latency of around 4 cycles whether int or float, so x*x*x will clock around 12 cycles. The main instruction needed for pow, F2XM1, costs anywhere from 50 cycles to 120, depending on the cpu. And then you need to do a bunch of other stuff to make F2XM1 handle different bases. |
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jimbob | Jimbob wrote:
> "bearophile" <bearophileHUGS@lycos.com> wrote in message news:h5h3uf$23sg$1@digitalmars.com...
>> Lars T. Kyllingstad:
>>> He also proposed that the overload be called opPower.
>> I want to add to two small things to that post of mine:
>> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=95123
>>
>> The name opPow() may be good enough instead of opPower().
>>
>> And A^^3 may be faster than A*A*A when A isn't a simple number, so always replacing the
>> power with mults may be bad.
>
> It wont be on x86. Multiplication has a latency of around 4 cycles whether int or float, so x*x*x will clock around 12 cycles.
Yeah, but what's the throughput? With multiple ALUs you can get several multiplications fast, even though getting the first one incurs a latency.
Andrei
|
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | Lars T. Kyllingstad wrote:
> Neither of the natural candidates, a^b and a**b, are an option, as they are, respectively, already taken and ambiguous.
I think that a ** b can be used, is not ambiguous except for the tokenizer of the language. It is the same difference you have with:
a ++ b -> identifier 'a', unary operator '++', identifier 'b' (not
parseable)
a + + b -> identifier 'a', binary operator '+', unary operator '+',
identifier 'b' (parseable)
I don't know anyone who writes ** to mean multiplication and dereference, except when obfuscating code. People usually prefer adding a whitespace between both operators, for obvious readability purposes.
I think it is perfectly reasonable to deprecate current usage of '**' for the next release, and a few releases later, make '**' a new operator. I doubt anyone would notice.
Other examples:
a-- - b
a - --b
a && b
a & &b
|
August 07, 2009 Re: Exponential operator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lars T. Kyllingstad | On 2009-08-07 06:50:25 -0400, "Lars T. Kyllingstad" <public@kyllingen.NOSPAMnet> said: > Daniel Keep has proposed the syntax > > a*^b > > while my suggestion was > > a^^b I always wondered why there isn't an XOR logical operator. binary logical (a & b) => (a && b) (a | b) => (a || b) (a ^ b) => (a ^^ b) -- Michel Fortin michel.fortin@michelf.com http://michelf.com/ |
Copyright © 1999-2021 by the D Language Foundation