www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - pow() function question

reply aeschere <aeschere nospam.cryptobit.org> writes:
I am relatively new to D.  So far, I really like it.  However, I need to
 do some power functions on integers.  Is pow() the only way of
performing this type of function? It seems silly to have to either use
reals for integer work.  This was one of my gripes with C's pow()
function.  I check the news archives and noticed there was talk of
creating a '**' power function for integers back in 2005. I tried it but
it didn't work.

Michael
(remove nospam if you need to get a hold of me personally)
Nov 27 2007
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"aeschere" <aeschere nospam.cryptobit.org> wrote in message 
news:fiheps$2m28$1 digitalmars.com...
I am relatively new to D.  So far, I really like it.  However, I need to
 do some power functions on integers.  Is pow() the only way of
 performing this type of function? It seems silly to have to either use
 reals for integer work.  This was one of my gripes with C's pow()
 function.  I check the news archives and noticed there was talk of
 creating a '**' power function for integers back in 2005. I tried it but
 it didn't work.

 Michael
 (remove nospam if you need to get a hold of me personally)

If you're using phobos, std.math.pow has an overload for integral exponents. Although I think it's only highly optimized for lower powers, like up to the third or fourth; anything higher and I think it uses floating point numbers. Keep in mind that floating point performance on modern processors is not exactly slow. Doing a power operation (which involves.. what, a ln? something like that) might be faster than a loop that multiplies the integers over and over.
Nov 27 2007
parent reply aeschere <aeschere nospam.cryptobit.org> writes:
Thanks for the help. I am currently using GDC compiler. I am not sure if
I am using phobos or not.  I will try to see what std.math.pow gives me.

Michael
Jarrett Billingsley wrote:
 "aeschere" <aeschere nospam.cryptobit.org> wrote in message 
 news:fiheps$2m28$1 digitalmars.com...
 I am relatively new to D.  So far, I really like it.  However, I need to
 do some power functions on integers.  Is pow() the only way of
 performing this type of function? It seems silly to have to either use
 reals for integer work.  This was one of my gripes with C's pow()
 function.  I check the news archives and noticed there was talk of
 creating a '**' power function for integers back in 2005. I tried it but
 it didn't work.

 Michael
 (remove nospam if you need to get a hold of me personally)

If you're using phobos, std.math.pow has an overload for integral exponents. Although I think it's only highly optimized for lower powers, like up to the third or fourth; anything higher and I think it uses floating point numbers. Keep in mind that floating point performance on modern processors is not exactly slow. Doing a power operation (which involves.. what, a ln? something like that) might be faster than a loop that multiplies the integers over and over.

Nov 27 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"aeschere" <aeschere nospam.cryptobit.org> wrote in message 
news:fihkob$2vlc$1 digitalmars.com...
 Thanks for the help. I am currently using GDC compiler. I am not sure if
 I am using phobos or not.  I will try to see what std.math.pow gives me.

If you're not using Tango, you're using phobos.
Nov 27 2007