473,811 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

negative exponent

10 New Member
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 6936
Laharl
849 Recognized Expert Contributor
AIR is not initialized, so (1+AIR) is most likely 1.
Dec 9 '07 #2
Ganon11
3,652 Recognized Expert Specialist
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
thatwhiteguy
10 New Member
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 Recognized Expert Contributor
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
thatwhiteguy
10 New Member
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 Recognized Expert Contributor
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
thatwhiteguy
10 New Member
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
thatwhiteguy
10 New Member
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 Recognized Expert Moderator Specialist
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

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

Similar topics

25
2997
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 the testing of it I discovered that negative numbers were being treated as if they were > 0. I compiled the following on mingw compiler/dev c++/windows xp. If I replace string.size() for a ordinary number it behaves as expected? I notice...
1
4902
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 expected number of digits for the exponent, I would like to control it. I can control the width of the base part but not the exponent. Does anybody know how to set the width of the exponent? TIA.
29
16878
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
5354
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 exponent and mantissa parts of a floating point number from its Q format representation.
2
3260
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' % (1.0e50) 1e+050 I would prefer two digits in the exponent. Usually 3 digits is not a problem, except for waisted disk space in 99.999999% of all practical
8
12315
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
2823
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
9853
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 appreciated.
3
4331
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 exponent digits used without affecting the operation of "g" in any other way? I need it to use 3 digits instead. Thanks, Ray
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.