468,457 Members | 1,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

negative exponent

I am trying to create a function that computes a negative exponent. This is what I have, but it is not returning the proper answer. I was wondering if anyone can see where I have gone wrong. Thanks

//This function raises 1+AIR to a negative n power

float power (int num_of_pay, float number)
{
float result;
int i;
double AIR;



number = (1 + AIR);

result = 1.0;
for (i=0; i< abs (num_of_pay); i++) result *= number;
if (num_of_pay > 0)
return (result);
else
return (1.0 / result);
}
Dec 9 '07 #1
10 6429
Laharl
849 Expert 512MB
AIR is not initialized, so (1+AIR) is most likely 1.
Dec 9 '07 #2
Ganon11
3,652 Expert 2GB
Actually, AIR is not initialized, so (AIR+1) is most likely bldsfkjdsad. It's useless, meaningless, has some random value that means nothing.
Dec 9 '07 #3
Alright, now that i fixed this, I am still coming up with the same answer no matter what data I input. I need for this function to be able to perform a negative exponentiation for a monthly payment calculator.
Dec 9 '07 #4
Laharl
849 Expert 512MB
Based on the code above, you set number to one, then multiply result by it n times. Thus, nothing happens. Simply multiply result by a double cast from number n times.
Dec 9 '07 #5
here is the code that I have revised to. no matter what my input it always gives me the same answer. I am not sure what I am doing wrong.

#include <iostream>

using namespace std ;
//function prototype
double payment(double balance, double AIR, int num_of_pay);
//funciton prototyp
double power (int num_of_pay, double AIR, float number);
int main ()
{
double month_pay; //
double balance; //amount of loan
double AIR; //annual interest rate
int num_of_pay ; //number of payments over course of loan
double result;

cout<< "Welcome to the loan payment calculator program!"<<endl ;

cout<< " Please enter the loan amount:"<<endl ;
cin>> balance;

cout<< " Please enter the annual interest rate as a percentage, not a decimal:"<<endl;
cin>> AIR;

cout<< "Please enter number of payments you will pay:"<<endl;
cin>> num_of_pay;

cout<< "The amount of your monthly payment is: $" << month_pay <<endl;

system("PAUSE");
return (0);
}

//************************************************** ***********


/************************************************** **/

double payment(double balance, double AIR, int num_of_pay)

/*This function computes the*/
/*amount of monthly payments*/
/*inputs???????????????????*/
/*balance, air, num_of_pay?*/
/*(double)?????????????????*/
/* outputs?????????????????*/
/*monthly payment??????????*/

{

double numerator;
double denominator;
double result;
double month_pay;

AIR = (AIR/100)/12;

numerator = balance*AIR;

denominator = (1 -result);

month_pay = (numerator/denominator);

return (month_pay);
}


//************************************************** ***************


/************************************************** **/

//This function raises 1+AIR to a negative n power

double power (int num_of_pay, double AIR, float number)
{
float result;
int i;

number = (1 + AIR);

result = 1.0;
for (i=0; i< abs (num_of_pay); i++)
if (num_of_pay > 0)

return (result);
else
return (1.0 / result);
}
Dec 9 '07 #6
Laharl
849 Expert 512MB
First off, you never actually call the functions you've written.

Second, in power, you check if num_of_pay is positive, and if it is, you immediately return result. If it isn't, you immediately return 1/result. You're missing the actual multiplications that make up the power. And since result is 1, either 1 and 1/1 are 1.
Dec 9 '07 #7
I see what you are saying about there being no multiplication, and so i changed the while loop to ;for (i=0; i< abs (num_of_pay); i++) result *= number;

I don not know what you mean about me not calling my functions. I was wondering if you could elaborate for me. Thank you
Dec 9 '07 #8
alright, I have taken your advice, and here is the revised code. I am getting an error message. It says [Warning] in function 'double: I have no idea why it is saying this.


#include <iostream>

using namespace std ;
//function prototype
double payment(double balance, double AIR, int num_of_pay);
//funciton prototyp
double power (int num_of_pay, double AIR, float number);
int main ()
{
double month_pay; //
double balance; //amount of loan
double AIR; //annual interest rate
int num_of_pay ; //number of payments over course of loan
double result;

cout<< "Welcome to the loan payment calculator program!"<<endl ;

cout<< " Please enter the loan amount:"<<endl ;
cin>> balance;

cout<< " Please enter the annual interest rate as a percentage, not a decimal:"<<endl;
cin>> AIR;

cout<< "Please enter number of payments you will pay:"<<endl;
cin>> num_of_pay;
month_pay = payment(balance, AIR, num_of_pay);
cout<< "The amount of your monthly payment is: $" << month_pay <<endl;

system("PAUSE");
return (0);
}

//************************************************** ***********


double power (int num_of_pay, double AIR, float number);
double payment(double balance, double AIR, int num_of_pay)

/*This function computes the*/
/*amount of monthly payments*/
/*inputs???????????????????*/
/*balance, air, num_of_pay?*/
/*(double)?????????????????*/
/* outputs?????????????????*/
/*monthly payment??????????*/

{

double numerator;
double denominator;
double result;
double month_pay;

AIR = (AIR/100)/12;

denominator = power (num_of_pay, AIR, number);

numerator = balance*AIR;

denominator = (1-denominator);

return (numerator/denominator);
}


//************************************************** ***************



//This function raises 1+AIR to a negative n power
double power (int num_of_pay, double AIR, float number)

{
float result;
int i;

number = (1 + AIR);

result = 1.0;

for (i=0; i< abs (num_of_pay); i++) result *= number;
if (num_of_pay > 0)
return (result);
else
return (1.0 / result);
}
Dec 10 '07 #9
sicarie
4,677 Expert Mod 4TB
thatwhiteguy-

Just an aside, if you are going to post code, please use the code tags provided. They are on the right as you create or reply to a post, and also in our Posting Guidelines.

Thanks!
Dec 10 '07 #10
Laharl
849 Expert 512MB
What warning is it giving you and which line is it in? Your compiler should give you that information.

Also, why do you have the parameter 'number' when it is not used for anything that a local variable for the function couldn't do. A double is probably a better choice than a float, and may will fix a loss of precision warning.
Dec 10 '07 #11

Post your reply

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

Similar topics

1 post views Thread by adahili | last post: by
29 posts views Thread by Peter Ammon | last post: by
2 posts views Thread by Jens Bloch Helmers | last post: by
8 posts views Thread by better_cs_now | last post: by
10 posts views Thread by schaefer.mp | last post: by
3 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.