www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Custom Exponents

reply "Malkierian" <rhydonj gmail.com> writes:
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
next sibling parent "Malkierian" <rhydonj gmail.com> writes:
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
prev sibling next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
prev sibling next sibling parent reply "Nathan M. Swan" <nathanmswan gmail.com> writes:
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
next sibling parent "Malkierian" <rhydonj gmail.com> writes:
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.
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.
Dec 14 2013
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:


 See the fourth overload: `if (isFloatingPoint!F && isFloatingPoint!G)`.
Is there any difference between using `a ^^ b` and `pow(a,b)`?
Dec 14 2013
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 12/15/13, Philippe Sigaud <philippe.sigaud gmail.com> wrote:


 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..).
Dec 15 2013
prev sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
 -----
 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
parent Marco Leise <Marco.Leise gmx.de> writes:
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
Dec 15 2013
prev sibling parent Marco Leise <Marco.Leise gmx.de> writes:
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