Custom Exponents

Marco Leise Marco.Leise at gmx.de
Sun Dec 15 06:22:11 PST 2013


Am Sun, 15 Dec 2013 06:22:45 +0100
schrieb "Malkierian" <rhydonj at 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



More information about the Digitalmars-d-learn mailing list