Pow operator precedence
Alvaro
alvaroDotSegura at gmail.com
Sat Jan 14 13:07:24 PST 2012
El 13/01/2012 21:29, bearophile escribió:
> Bioinformatics, exploratory programing, simulations, data munging, hardening of slow scripts, data visualization, data mining, optimization of some tasks, faster routines for dynamic code written by other people, and more.
>
> The problem is that often you don't have a "x", but a "long_named_variable", ...
> ...
>> Realistically, how often do you cube? It's extremely rare in my experience.
>
> ^^3 is not common.
I completely agree. The ^^ operator is a great addition in D even if it
is not used very often. I always found awkward the lack of such operator
in C-derived languages (some others use either ^ or **). And I was happy
to see in the DMD source how ^^2 and ^^3 are rewritten to avoid pow().
There are indeed applications that use exponentiation and whose
expressions using ^^ are much more readable and closer to the
mathematical notation.
e.g. sphere_volume = 4./3 * PI * radius^^3; // so much better
As for the original discussion on precedence I think the first example
is different to the others posted in that it uses a *number literal*.
in -x^^2, following standard math, it should be -(x^^2) (<0)
but the first example, -10^^2 is confusing because one might quickly see
the - sign as part of the number literal. As long as in D the - sign is
not part of the literal, the current behavior is fine. If you want it
the other way around write:
(-10)^^2
More information about the Digitalmars-d
mailing list