473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 49218
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: David | last post by:
How can you write a program in C++ by using loop to control the calculation without the use of library function pwr .For example 3 to the power of 4 (ie 3x3x3x3). Any help will be appreciated
2
by: Alex Vinokur | last post by:
Does STL contain algorithms which generate (enable to generate) exponents, permutations, arrangements, and combinations for any numbers and words? -- Alex Vinokur mailto:alexvn@connect.to...
8
by: Shawn B. | last post by:
Greetings, I have the expression that works in Excel, Javascript, and VB.NET: B1 = 18 10^(3*(B1)/1130)*6 Result: 6.697903112
2
by: Stefan L | last post by:
Hi NG, I have the following problem: Is there a way to use exponents in C#-code without converting to a double and then use System.Math? I have to convert VB-Code to C# and in VB its easy...
4
by: enigmadude | last post by:
As many have heard, IronPython 1.0 was released. When I was looking through the listed differences between CPython and IronPython, the document mentioned that using large exponents such as 10 **...
12
by: zalery | last post by:
so i'm trying to set up this exponents loop, keep in mind this is my first year in computer science so my knowledge of script is somewhat minimal. basically this assignment (or at least part of it)...
2
jkmyoung
by: jkmyoung | last post by:
Calculating Large Exponents Background: This is a quick article as to how to calculate the exponents of large numbers quickly and efficiently. Starting with a basic multiplication algorithm, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.