Exponential operator

Bill Baxter wbaxter at gmail.com
Fri Aug 7 04:30:06 PDT 2009


On Fri, Aug 7, 2009 at 3:50 AM, Lars T.
Kyllingstad<public at 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



More information about the Digitalmars-d mailing list