Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

benlangdon

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
how do you use pow?

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

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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:-
Code:
man pow

b e n
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
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:-
Code:
man pow

b e n

wow never knew that?

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

benlangdon

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
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)
 

benlangdon

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
Picture1-9.png


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

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

Moderator emeritus
Mar 10, 2004
14,672
1,492
Bergen, Norway
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:

Code:
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

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
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
Code:
#include <iostream>[B]#include <math.h>[/B]

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;
		[B]rateTerm = 
		powf(float rate,float term);[/B]
		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

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

Moderator emeritus
Mar 10, 2004
14,672
1,492
Bergen, Norway
Code:
#include <iostream>[B]#include <math.h>[/B]

using namespace std;
Try one line per include statement:
Code:
#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

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
Picture1-10.png


Code:
#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
 

benlangdon

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
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

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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

macrumors 65816
Original poster
Jan 13, 2008
1,497
0
Picture22-1.png

Code:
#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

macrumors regular
Jan 17, 2007
170
0
Code:
		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.
 

Sbrocket

macrumors 65816
Jun 3, 2007
1,250
0
/dev/null
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.