473,324 Members | 2,196 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,324 software developers and data experts.

Using a variable as a power

How can I use a variable as a power in C++

for example an equation n^n
should pow(n ,n) work?

I have a program for a class where a variable FIRST is the first number inputted from a file. I need to then work out the factorial using the equation
n! = e^(-n)*(n^n)*sqrt(2*pi*n)

Using FIRST as my variable instead of N I came up with the following equation:
FACTORIAL = (((pow(2.71828183, (-FIRST))) * (pow(FIRST, FIRST)) * sqrt(2 * 3.14159265 * FIRST)));

I am getting some overloading errors which I believe are due to the fact that I used a variable as the power instead of an integer?

How would one go about writing this equation using FIRST as my variable for N (FIRST happens to equal 6 if that makes any difference)?

This is my first C++ class so I am new to this. Any help would be great! thanks.
Oct 2 '08 #1
4 5849
newb16
687 512MB
I am getting some overloading errors...
Works fine for me, and probably for others too. So you have to post verbatim text of these errors.
Oct 2 '08 #2
archonmagnus
113 100+
Just a quick thought...

You may be getting overloading of the "pow" function. Look up the function declarations of pow() relative to the variable type of "FIRST" (in your case). In other words, you may have to cast "FIRST" to either a float or a double to match one of the overloads.

For example:
Expand|Select|Wrap|Line Numbers
  1. FACTORIAL = (pow(2.71828183, (double)(-FIRST))
  2.                   * pow((double)FIRST, (double)FIRST)
  3.                   * sqrt(2.0 * 3.14159265 * (double)FIRST));
  4.  
As an aside, the obsessive-compulsive side of me wanted to fix your "mixed-mode" operations as well (integers multiplied with doubles, etc.). It's really not that big of a deal, but like I said, I have mild OCD.
Oct 2 '08 #3
Thank you. It was overloading the pow() function. It is now working correctly. So when using a variable as the exponent or power you should define it as a double?
FIRST is defined as a float at the beginning of the code.
Oct 3 '08 #4
boxfish
469 Expert 256MB
pow can take either two doubles or two floats. Reference page on pow.
Oct 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sky2070 | last post by:
#register variable session_register("product1"); session_register("product2"); $product1="Palm pilot"; $product2="Power mac 5"; #print variable from another page print "Your chosen products...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
52
by: mavic | last post by:
hi to everybody,i need help for my project....how to make a program that accept any integer and convert it binary,octal and hexadecimal.pls help me...thanks
13
by: Andy Baxter | last post by:
Can anyone recommend a good online guide to using objects in javascript? The book I bought (DHTML Utopia) suggests using objects to keep the code clean and stop namespace clashes between different...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
0
by: outofstep | last post by:
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here. I'm using DEV C++ as my compiler. I'm trying to calculate...
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
7
by: yodadbl07 | last post by:
hey im trying to write a class of polynomials using a linked list. But I am getting an error at run time with my Polynomial* getNext() function. The error says access violation reading location. Im...
13
by: jkimbler | last post by:
As part of our QA of hardware and firmware for the company I work for, we need to automate some testing of devices and firmware. Since not everybody here knows C#, I'm looking to create a new...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.