472,779 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 6831
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

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

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
1
by: adahili | last post by:
Hi, I use cout to print numbers in scientific notation. I noticed that a number like 12.34 is sometimes (compiler-dependent) printed as 1.234e+1 or 1.234e+01 or 1.234e+001. Since I know the...
29
by: Peter Ammon | last post by:
What's the most negative double value I can get? Is it always guaranteed to be -DBL_MAX? If so, why (or rather, where)? Thanks.
5
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the...
2
by: Jens Bloch Helmers | last post by:
How can I control the number of digits in the exponent when writing floats to a file? It seems that Python2.4.2(winXP) prints three digits anyway. >>> print 1.0e50 1e+050 >>> print '%e' %...
8
by: better_cs_now | last post by:
Hello all, I'd like to get the negative value of largest possible magnitude for a float. I've considered: -numeric_limits<float>::max() and -numeric_limits<float>::infinity()
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
10
by: schaefer.mp | last post by:
Does anyone know of an approximation to raising a negative base to a fractional exponent? For example, (-3)^-4.11111 since this cannot be computed without using imaginary numbers. Any help is...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, By default, the "g" format specifier seems to use 2 digits in the exponent if it decides to use the scientific format. I.e., Double.ToString("g"). How do I control the number of...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.