digitalmars.D.learn - Custom Exponents
- Malkierian (7/7) Dec 14 2013 Alright, so I'm trying to do hex string to integer conversion,
- Malkierian (4/12) Dec 14 2013 Well, I made my own pow function to use for now, but it seems
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/10) Dec 14 2013 There is an operator for that: :)
- Nathan M. Swan (4/10) Dec 14 2013 http://dlang.org/phobos/std_math.html#.pow
- Malkierian (4/20) Dec 14 2013 Gah, why didn't I search pow in that page... I HAD looked there
- Philippe Sigaud (1/3) Dec 14 2013 Is there any difference between using `a ^^ b` and `pow(a,b)`?
- Andrej Mitrovic (47/52) Dec 15 2013 Depends on whether the arguments are known at compile-time, and their
- Philippe Sigaud (3/14) Dec 15 2013 Ugh. I agree.
- Marco Leise (5/16) Dec 15 2013 dmd lacks a native implementation of ^^ and replaces it with pow() or so...
- Marco Leise (14/21) Dec 15 2013 Hi. I'm an efficiency guy. May I suggest using an algorithm
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.
Dec 14 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.
Dec 14 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
Dec 14 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.See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`. NMS
Dec 14 2013
On Sunday, 15 December 2013 at 05:39:03 UTC, Nathan M. Swan wrote:On 12/14/13 8:22 PM, Malkierian wrote: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.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.See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`. NMS
Dec 14 2013
See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.Is there any difference between using `a ^^ b` and `pow(a,b)`?
Dec 14 2013
On 12/15/13, Philippe Sigaud <philippe.sigaud gmail.com> wrote: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..).See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.Is there any difference between using `a ^^ b` and `pow(a,b)`?
Dec 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?
Dec 15 2013
Am Sun, 15 Dec 2013 13:52:55 +0100 schrieb Philippe Sigaud <philippe.sigaud gmail.com>:dmd lacks a native implementation of ^^ and replaces it with pow() or something. -- Marco----- 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?
Dec 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
Dec 15 2013