PDA

View Full Version : c++ using pow(x,y)




benlangdon
Mar 30, 2008, 01:24 AM
how do you use pow?

variables
-rate
-term
i need ( 1 + rate) to the exponent of term



Sbrocket
Mar 30, 2008, 02:03 AM
pow(1+rate,term);

Your variables should be of type double, or you'll need to cast them like so.

pow(1+(double)rate,(double)term);

pow() returns a double, also.

That simple. When in doubt, try consulting the documentation.

benlangdon
Mar 30, 2008, 05:13 AM
my teacher just told us about it and didn't give any paper on it.

thanks.

i have never used double but will give it a try.

when i declare the variables should i use float or...

lazydog
Mar 30, 2008, 05:35 AM
Hi,

You might like to know that powf() is a 'float' version of pow().

To get some info on pow, type this in a terminal window:-

man pow


b e n

toddburch
Mar 30, 2008, 01:31 PM
http://www.cppreference.com/stdmath/pow.html

Todd

Sbrocket
Mar 30, 2008, 02:02 PM
my teacher just told us about it and didn't give any paper on it.

thanks.

i have never used double but will give it a try.

when i declare the variables should i use float or...

Yeah, I forgot to mention...you can also use different versions of the same function if for some reason you don't want to use double.

There's "powf(x,y)" for float types, and "powl(x,y)" for long double precision numbers if for some reason you need that high precision.

As lazydog mentioned, the man page for pow contains this and a lot more information about the function. Man pages for standard c functions are good references if you have questions about other functions (the "documentation" I referred to) and you can get to them by opening up a Terminal.app window and typing "man <function>" (e.g. "man pow").

If you want to use floats, you could declare your variables as float and then just use "powf(1+rate, term);"

benlangdon
Mar 30, 2008, 02:05 PM
Hi,

You might like to know that powf() is a 'float' version of pow().

To get some info on pow, type this in a terminal window:-

man pow


b e n

wow never knew that?

thanks guys for the help, hopefully i can get this running soon

benlangdon
Mar 30, 2008, 02:53 PM
http://i244.photobucket.com/albums/gg2/benlangdon/Picture1-7.png

wow i just cannot get it to work
how are you supposed to write it
i need the pow(x,Y) in another equation
all those guilds do not show me how to actually write it
it only works if it goes pow(x,y) not pow(1+x,y)

Mitthrawnuruodo
Mar 30, 2008, 03:14 PM
#include <math.h>

benlangdon
Mar 30, 2008, 03:20 PM
#include <math.h>


where?

i just made it like 10 times more complicated
i am getting pissed off now :mad:
i need a idiots guild to this
http://i244.photobucket.com/albums/gg2/benlangdon/Picture1-8.png

benlangdon
Mar 30, 2008, 03:33 PM
http://i244.photobucket.com/albums/gg2/benlangdon/Picture1-9.png

well thats pretty much it i guess
now if it would work i could adjust things
:mad:

Mitthrawnuruodo
Mar 30, 2008, 03:37 PM
where?Where do you usually put your includes...?

Sbrocket
Mar 30, 2008, 03:40 PM
I'm surprised your professor told you to use pow() and didn't mention including math.h or anything about how to use it.

You need to add "#include <math.h>" after your "#include <iostream>" statement.

Mitthrawnuruodo
Mar 30, 2008, 03:43 PM
I'm surprised your professor told you to use pow() and didn't mention including math.h or anything about how to use it.
Well, it shouldn't be that magic and impossible to find, and the above mentioned man page is extremely clear:

POW(3) BSD Library Functions Manual POW(3)

NAME
pow -- power function

SYNOPSIS
#include <math.h>

double
pow(double x, double y);

long double
powl(long double x, long double y);

float
powf(float x, float y);

DESCRIPTION
The pow() functions compute x raised to the power y.


Part of learning basic programming is more to figure out where to obtain information than to be spoon-fed the code... </old grumpy ex-teacher in c++> ;)

benlangdon
Mar 30, 2008, 03:45 PM
Where do you usually put your includes...?

what?

going back to try it out


Part of learning basic programming is more to figure out where to obtain information than to be spoon-fed the code... </old grumpy ex-teacher in c++> ;)

so...
spoon feed is good?

i mean i can do this stuff if i need to massage things out, but doing it from scratch is... its my first time from scratch

edit:
and i still cannot get it

#include <iostream>#include <math.h>

using namespace std;

int main ( void )
{
// Name - Class - Assignment - Due Date
cout<< "Ben Langodon\n Computer Science\nHomework 10\n4\\3\\08\n\n";

// Variables
float rate; // Annual Interest rate
float rate1; // rate + 1
float term; // Term of loan in years
float term1; // term * 12
float moneyBorrowed; // Amount of money borrowed
float monthlyPay; // Amount paid every month
float rateTerm; // pow( 1 + rate, term)

// Defining of Variables
cout<<"Annual Interest Rate:";
cin>>rate;
cout<<"Term of Loan in Years:";
cin>>term;
cout<<"Amount of Money Borrowed:";
cin>>moneyBorrowed;

// Process
term1 = term * 12;
rate1 = rate + 1;
rateTerm =
powf(float rate,float term);
monthlyPay = rate1 * rateTerm / rateTerm - 1 * moneyBorrowed;

// Output
cout<<"Amount paid a month:"<<monthlyPay;
}


the bold is what i changed, i tried it all on one line also
like this, i just took this code from your post, [the code and /code]

Sbrocket
Mar 30, 2008, 03:50 PM
Part of learning basic programming is more to figure out where to obtain information than to be spoon-fed the code... </old grumpy ex-teacher in c++> ;)

As a professor and experienced programmer, you would say that...but I assume the professor is there for a reason. Reminding students of the necessity is different from spoon-feeding code, and (believe it or not) not all professors are the best around when it comes to providing students with the tools of the trade. I mean, he didn't even seem to know about finding docs through the man page and I bet that most people new to C++ (such as taking an introductory course, which is level of program would seem to be from) would gloss over the #include statement. </current grumpy student>

Mitthrawnuruodo
Mar 30, 2008, 04:03 PM
#include <iostream>#include <math.h>

using namespace std;

Try one line per include statement:
#include <iostream>
#include <math.h>



As a professor and experienced programmer, you would say that...Not professor, nor experienced...just taught some lessons in very basic c++ at the local College when I continued (with something completely different) at University... ;)

benlangdon
Mar 30, 2008, 04:09 PM
http://i244.photobucket.com/albums/gg2/benlangdon/Picture1-10.png


#include <iostream>
#include <math.h>

using namespace std;

int main ( void )
{
// Name - Class - Assignment - Due Date
cout<< "Ben Langodon\n Computer Science\nHomework 10\n4\\3\\08\n\n";

// Variables
float rate; // Annual Interest rate
float rate1; // rate + 1
float term; // Term of loan in years
float term1; // term * 12
float moneyBorrowed; // Amount of money borrowed
float monthlyPay; // Amount paid every month
float rateTerm; // pow( 1 + rate, term)

// Defining of Variables
cout<<"Annual Interest Rate:";
cin>>rate;
cout<<"Term of Loan in Years:";
cin>>term;
cout<<"Amount of Money Borrowed:";
cin>>moneyBorrowed;

// Process
term1 = term * 12;
rate1 = rate + 1;
rateTerm = powf(float rate,float term);
monthlyPay = rate1 * rateTerm / rateTerm - 1 * moneyBorrowed;

// Output
cout<<"Amount paid a month:"<<monthlyPay;
}


still not working
and i haven't even put the 1 in
rateTerm = powf(float 1 + rate,float term);





and btw guys i thank you so much for helping me so far
i know i am being a nuisance, but thanks

Cromulent
Mar 30, 2008, 04:35 PM
Why are you declaring rate and term twice, or at least trying to? Delete the two float words in the powf() function.

benlangdon
Mar 30, 2008, 04:39 PM
Why are you declaring rate and term twice, or at least trying to? Delete the two float words in the powf() function.

well i was trying to say that
rateTerm = 1 + rate to the exponent of term

so them combined i figure would be called rateTerm since its rate and term


omg it worked
f ya
now to debug

thank you guys so much

Sbrocket
Mar 30, 2008, 06:17 PM
well i was trying to say that
rateTerm = 1 + rate to the exponent of term

so them combined i figure would be called rateTerm since its rate and term

That's not he was referring to.

This line: rateTerm = powf(float rate, float term);
Should be: rateTerm = powf(rate, term);

... because you are declaring the 'rate' and 'term' variables twice, not to mention that declaration in that form wouldn't work even if they weren't already declared previously. But it looks like you already figured that out.

benlangdon
Mar 30, 2008, 06:34 PM
http://i244.photobucket.com/albums/gg2/benlangdon/Picture22-1.png

#include <iostream>
#include <math.h>
#include<iomanip>

using namespace std;

int main ( void )
{
// Name - Class - Assignment - Due Date
cout<< "Ben Langodon\nComputer Science\nHomework 10\n4\\3\\08\n\n";


// Variables
float rate; // Annual Interest rate
float moneyBorrowed; // Amount of money borrowed
float monthlyPay; // Amount paid every month
float rateTerm; // pow( 1 + rate, term)
// Term in years - Term in months
float term; // Term of loan in years
int term1; // term * 12
// formula variables
float a1; // rate * rateTerm
float a2; // rateTerm - 1
float a3; // a1 / a2


// setpricision
setprecision(2);


// Defining of Variables
cout<<"Amount of Money Borrowed:"<<setw(25);
cin>>moneyBorrowed;
cout<<"Annual Interest Rate:"<<setw(25);
cin>>rate;
cout<<"Term of Loan in years:"<<setw(25);
cin>>term;

// Term * 12, years to months
term1 = term*12;

// 2nd varaibles
cout<<"Term of Loan in months:"<<term1;


// Process
rateTerm = powf(1 + rate,term1);
a1 = rate * rateTerm;
a2 = rateTerm - 1;
a3 = a1/a2;
monthlyPay = a3*moneyBorrowed;


// Output
cout<<setw(25)<<"\n\nAmount paid a month:"<<monthlyPay;
}


well i tried to tweak the processes area from having all as one problem to parts of the problem
it still didn't work
on the left is my work,
right top is the assignment,
right bottom is the compiled program as it ran

the values we are supposed to use are
money borrowed (moneyBorrowed) : 120,000
interest rate (rate) : 7
term in years : 30

and the answer is supposed to be 798.36

i will rewrite the processes part without the variables and post that if anyone wants it like that

CaptainZap
Mar 30, 2008, 06:49 PM
rateTerm = powf(1 + rate,term1);
a1 = rate * rateTerm;
a2 = rateTerm - 1;
a3 = a1/a2;
monthlyPay = a3*moneyBorrowed;

Try figuring out where the calculations are going wrong, put a line that prints out the variable after each calculation so you can figure out where the problem is.

Cromulent
Mar 30, 2008, 07:29 PM
Your powf() function is still wrong. You need to keep an eye on the types you are using, or at least cast if you want to treat them as a different type.

Sbrocket
Mar 30, 2008, 07:38 PM
Your powf() function is still wrong. You need to keep an eye on the types you are using, or at least cast if you want to treat them as a different type.

His powf() isn't wrong as far as I see, he's using floats for everything.

EDIT: Nevermind, I missed the int declaration. benlangdon - Why exactly are you using int for term1?

EDIT2: Are you sure 798.36 is the correct result? Doing the calculation on my TI-89 resulted in 8400 which is also what I get upon compiling and running your code. By the way - if you're going to input the monthly rate as a percentage as the assignment says, then you're going to need to convert it to a decimal value first. (1 + rate) is not supposed to be (1 + 7), its supposed to be (1 + 0.07). You corrected this by inputting .07 as your input in Xcode, but technically that's not correct as the assignment says to do it.

benlangdon
Mar 30, 2008, 08:08 PM
His powf() isn't wrong as far as I see, he's using floats for everything.

EDIT: Nevermind, I missed the int declaration. benlangdon - Why exactly are you using int for term1?

EDIT2: Are you sure 798.36 is the correct result? Doing the calculation on my TI-89 resulted in 8400 which is also what I get upon compiling and running your code. By the way - if you're going to input the monthly rate as a percentage as the assignment says, then you're going to need to convert it to a decimal value first. (1 + rate) is not supposed to be (1 + 7), its supposed to be (1 + 0.07). You corrected this by inputting .07 as your input in Xcode, but technically that's not correct as the assignment says to do it.

ya i changed it to int for some reason but i changed it back almost right after i posted this.

ya i worked it out to on my ti84 and got the same thing it was like 805 instead of 798
i give up i emailed my teacher with all this. i give up

CaptainZap
Mar 30, 2008, 09:05 PM
K, I've figured it out. You need to take the rate and divide it by 1200, so since the rate is 7, we divide 7/1200 = .005833333, let this value = X for now

So we do this
(X + 1) ^ 360 = 8.1165
8.1165 * X = .047346 this is the numerator

Now for the denominator
(1 + X) ^ 360 = 8.1165
8.1165 - 1 = 7.1165

So we end up with
.047346 / 7.1165 = .006653

Then take that number and multiply it by the loan amount
.006653 * 120,000 = 798.36

Phew, that was fun.
And to fix the code, just add this line right above the calculations
rate = rate / 1200;
rateTerm = powf(1 + rate,term1);
Except you get 3 decimal points, at least on my machine you do. But I don't know C++ so I can't fix it =D

Oh, and I used this website to find out that the rate has to be divided by 1200 http://www.1728.com/loanform.htm The equations are different but they come out the same.

benlangdon
Mar 30, 2008, 09:28 PM
K, I've figured it out. You need to take the rate and divide it by 1200, so since the rate is 7, we divide 7/1200 = .005833333, let this value = X for now

So we do this
(X + 1) ^ 360 = 8.1165
8.1165 * X = .047346 this is the numerator

Now for the denominator
(1 + X) ^ 360 = 8.1165
8.1165 - 1 = 7.1165

So we end up with
.047346 / 7.1165 = .006653

Then take that number and multiply it by the loan amount
.006653 * 120,000 = 798.36

Phew, that was fun.
And to fix the code, just add this line right above the calculations
rate = rate / 1200;
rateTerm = powf(1 + rate,term1);
Except you get 3 decimal points, at least on my machine you do. But I don't know C++ so I can't fix it =D

Oh, and I used this website to find out that the rate has to be divided by 1200 http://www.1728.com/loanform.htm The equations are different but they come out the same.


why are you dividing 7 by 1200?
i was thinking if seven wasn't a percent you still would * by 12 for months in a year.

edit: thanks for that site i guess i just did not know how to work the problem which is weird because she said that all the math was pretty basic as this is accounting

2nd edit
still do not understand why you divided by 1200

CaptainZap
Mar 30, 2008, 09:29 PM
why are you dividing 7 by 1200?
i was thinking if seven wasn't a percent you still would * by 12 for months in a year.

No idea mate, that's just what http://www.1728.com/loanform.htm said :P
Wait... I think it is because if you have 7, it is a percent, so divide that by 100, you get .07, then you have to separate it into months (maybe?) and then divide by 12. Or more succinctly, 7 / 1200. But I have no idea how loans work... so...

benlangdon
Mar 30, 2008, 09:32 PM
No idea mate, that's just what http://www.1728.com/loanform.htm said :P

i mean if it was .07% and not just 7 1200 would be like multiplying by 12 then adding 2 00's to not have a decimal so i get that but w/e

thanks

muuhhahahahaha
i got itttttttttttttttttt
it workssssssssssssssssssssss

thanks everyone.
captainzap thank you so much for helping me out with the equation and about cout after every processes worked

Sbrocket thanks for getting me started and teaching me pow

Mitthrawnuruodo thanks for clueing me in on something i missed

lazydog thanks for the info from terminal

toddburch thanks for the info

Cromulent thanks for helping me see something i missed

lee1210 thanks for helping me find my problem

Ben Langodon
Computer Science
Homework 10
4\3\08

Amount of Money Borrowed:120000
Annual Interest Rate:7
Term of Loan in years:30

Amount paid a month:798.361

now to clean it up

thank you guys so much