Custom Exponents

Andrej Mitrovic andrej.mitrovich at gmail.com
Sun Dec 15 02:45:30 PST 2013


On 12/15/13, Philippe Sigaud <philippe.sigaud at gmail.com> wrote:
>>
>> http://dlang.org/phobos/std_math.html#.pow
>>
>> See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.
>
> Is there any difference between using `a ^^ b` and `pow(a,b)`?

Depends on whether the arguments are known at compile-time, and their
type. And in fact.. constant folding also affects what works
regardless of what you type something with, so initializers are taken
into account. Some demonstrations:

-----
void main()
{
    int a = 2, b = 2;
    auto c = a ^^ b;
}
-----

test.d(6): Error: must import std.math to use ^^ operator

The following will work though, probably through some compiler intrinsic:

-----
void main()
{
    enum a = 2, b = 2;
    auto c = a ^^ b;
}
-----

Here's where things get interesting. The following works:

-----
void main()
{
    enum float a = 1.0;
    enum float b = 2.1;
    auto c = a ^^ b;
}
-----

But the following doesn't:

-----
void main()
{
    enum float a = 1.1;  // note the .1 change
    enum float b = 2.1;
    auto c = a ^^ b;
}
-----

So even if you type something as `enum float a = 1.0`, the compiler
will be able to implicitly convert it to an int and it will make the
call work.

It seems like the intrinsic supports int^^int, int^^float, and
float^^int, but not float^^float.

Side-note: Importing std.stdio implicitly imports std.math, so you
won't have any errors (boy do I hate public imports where they don't
belong..).


More information about the Digitalmars-d-learn mailing list