Thread overview
Custom Exponents
Dec 15, 2013
Malkierian
Dec 15, 2013
Malkierian
Dec 15, 2013
Ali Çehreli
Dec 15, 2013
Nathan M. Swan
Dec 15, 2013
Malkierian
Dec 15, 2013
Philippe Sigaud
Dec 15, 2013
Andrej Mitrovic
Dec 15, 2013
Philippe Sigaud
Dec 15, 2013
Marco Leise
Dec 15, 2013
Marco Leise
December 15, 2013
Alright, so I'm trying to do hex string to integer conversion, but I can't for the live of me find how to do exponent calculation in D.  Java has a Math.pow() function that allows you to specify the base and the exponent, but all I've seen in D's libraries are functions that allow you to only specify the exponent and does it on a predetermined base, such as 2, e or 10.  I need 16 for the base.
December 15, 2013
On Sunday, 15 December 2013 at 05:22:47 UTC, Malkierian wrote:
> Alright, so I'm trying to do hex string to integer conversion, but I can't for the live of me find how to do exponent calculation in D.  Java has a Math.pow() function that allows you to specify the base and the exponent, but all I've seen in D's libraries are functions that allow you to only specify the exponent and does it on a predetermined base, such as 2, e or 10.
>  I need 16 for the base.

Well, I made my own pow function to use for now, but it seems kind of stupid to me that that's not in the standard library.  I must be missing something.
December 15, 2013
On 12/14/2013 09:22 PM, Malkierian wrote:
> Alright, so I'm trying to do hex string to integer conversion, but I
> can't for the live of me find how to do exponent calculation in D.  Java
> has a Math.pow() function that allows you to specify the base and the
> exponent, but all I've seen in D's libraries are functions that allow
> you to only specify the exponent and does it on a predetermined base,
> such as 2, e or 10.  I need 16 for the base.

There is an operator for that: :)

    assert(16 ^^ 2 == 256);

Ali

December 15, 2013
On 12/14/13 8:22 PM, Malkierian wrote:
> Alright, so I'm trying to do hex string to integer conversion, but I
> can't for the live of me find how to do exponent calculation in D.  Java
> has a Math.pow() function that allows you to specify the base and the
> exponent, but all I've seen in D's libraries are functions that allow
> you to only specify the exponent and does it on a predetermined base,
> such as 2, e or 10.  I need 16 for the base.

http://dlang.org/phobos/std_math.html#.pow

See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.

NMS
December 15, 2013
On Sunday, 15 December 2013 at 05:39:03 UTC, Nathan M. Swan wrote:
> On 12/14/13 8:22 PM, Malkierian wrote:
>> Alright, so I'm trying to do hex string to integer conversion, but I
>> can't for the live of me find how to do exponent calculation in D.  Java
>> has a Math.pow() function that allows you to specify the base and the
>> exponent, but all I've seen in D's libraries are functions that allow
>> you to only specify the exponent and does it on a predetermined base,
>> such as 2, e or 10.  I need 16 for the base.
>
> http://dlang.org/phobos/std_math.html#.pow
>
> See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.
>
> NMS

Gah, why didn't I search pow in that page...  I HAD looked there before...

Exponent should be somewhere in the description of that function.
December 15, 2013
>
> 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)`?
December 15, 2013
On 12/15/13, Philippe Sigaud <philippe.sigaud@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..).
December 15, 2013
> -----
> void main()
> {
>     int a = 2, b = 2;
>     auto c = a ^^ b;
> }
> -----
>
> test.d(6): Error: must import std.math to use ^^ operator

?? The ^^ operator is defined in std.math?


> 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..).

Ugh. I agree.
How does std.stdio imports std.math?
December 15, 2013
Am Sun, 15 Dec 2013 13:52:55 +0100
schrieb Philippe Sigaud <philippe.sigaud@gmail.com>:

> > -----
> > void main()
> > {
> >     int a = 2, b = 2;
> >     auto c = a ^^ b;
> > }
> > -----
> >
> > test.d(6): Error: must import std.math to use ^^ operator
> 
> ?? The ^^ operator is defined in std.math?

dmd lacks a native implementation of ^^ and replaces it with pow() or something.

-- 
Marco

December 15, 2013
Am Sun, 15 Dec 2013 06:22:45 +0100
schrieb "Malkierian" <rhydonj@gmail.com>:

> Alright, so I'm trying to do hex string to integer conversion,
> but I can't for the live of me find how to do exponent
> calculation in D.  Java has a Math.pow() function that allows you
> to specify the base and the exponent, but all I've seen in D's
> libraries are functions that allow you to only specify the
> exponent and does it on a predetermined base, such as 2, e or 10.
>   I need 16 for the base.

Hi. I'm an efficiency guy. May I suggest using an algorithm that closer matches what the machine can do? Instead of:

  result += hexValue(s[i]) * 16 ^^ (s.length-1 - i);

you could write

  result *= 16;
  result += hexValue(s[i]);

since general exponent calculations are not supported by the integer arithmetic unit of the CPU, ^^ or pow will do a lot of extra multiplications when you could get away with only one each step.

-- 
Marco