469,571 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,571 developers. It's quick & easy.

C# and Exponents

Hi, quick question that's driving me nvts. how do i do exponential math in
C#?

--
Pat
Nov 17 '05 #1
6 48985
Patrick McGovern <pa**************@unisys.cm> wrote:
Hi, quick question that's driving me nvts. how do i do exponential math in
C#?


Use Math.Pow - there's no exponent operator in C# itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Patrick,

Take a look at the static Pow method on the Math class.

The bad thing here is that it only takes doubles as an argument.

If you want to use integral values, you could always write the algorithm
yourself (looping through n times, multiplying the result by itself).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Patrick McGovern" <pa**************@unisys.cm> wrote in message
news:dj**********@trsvr.tr.unisys.com...
Hi, quick question that's driving me nvts. how do i do exponential math in
C#?

--
Pat

Nov 17 '05 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Patrick,

Take a look at the static Pow method on the Math class.

The bad thing here is that it only takes doubles as an argument.

If you want to use integral values, you could always write the
algorithm yourself (looping through n times, multiplying the result by
itself).

Hope this helps.
that helped!!


--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Patrick McGovern" <pa**************@unisys.cm> wrote in message
news:dj**********@trsvr.tr.unisys.com...
Hi, quick question that's driving me nvts. how do i do exponential math
in C#?

--
Pat


Nov 17 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Patrick McGovern <pa**************@unisys.cm> wrote:
Hi, quick question that's driving me nvts. how do i do exponential math
in
C#?
Use Math.Pow - there's no exponent operator in C# itself.


thanks Jon!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5


"Nicholas Paldino [.NET/C# MVP]" wrote:
If you want to use integral values, you could always write the algorithm
yourself (looping through n times, multiplying the result by itself).

Hope this helps.


that's horridly inefficient with large exponents though. There's a much
better way to do integer exponents using shifting. Treat the exponent as a
string of bits. Then for each bit that has a value of 1, add the base
shifted left by the bit number. Ie N^13 (13 = 1101) = N + N shifted left 2
bits + N shifted left 3 bits. It doesn't matter much with small exponenets
but with big ones 32 shifts/adds pairs is far faster than millions of
multiplications.
Nov 17 '05 #6
for example N^24 = N * N ...... * N (24 times)
OR
N^24 = (((N^2)^2)^2)^3 it requires 5 multiplication rather then 24
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Shawn B. | last post: by
2 posts views Thread by Stefan L | last post: by
4 posts views Thread by enigmadude | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.