c++ using pow(x,y)

Discussion in 'Mac Programming' started by benlangdon, Mar 29, 2008.

  1. macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #1
    how do you use pow?

    variables
    -rate
    -term
    i need ( 1 + rate) to the exponent of term
     
  2. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #2
    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.
     
  3. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #3
    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...
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    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
     
  5. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  6. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #6
    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);"
     
  7. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #7
    wow never knew that?

    thanks guys for the help, hopefully i can get this running soon
     
  8. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #8
    [​IMG]

    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)
     
  9. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #9
    Code:
    #include <math.h>
    
     
  10. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #10
    where?

    i just made it like 10 times more complicated
    i am getting pissed off now :mad:
    i need a idiots guild to this
    [​IMG]
     
  11. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #11
    [​IMG]

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

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #12
    Where do you usually put your includes...?
     
  13. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #13
    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.
     
  14. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #14
    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++> ;)
     
  15. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #15
    what?

    going back to try it out

    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]
     
  16. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #16
    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>
     
  17. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #17
    Try one line per include statement:
    Code:
    #include <iostream>
    #include <math.h>
    

    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... ;)
     
  18. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #18
    [​IMG]

    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
     
  19. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #19
    Why are you declaring rate and term twice, or at least trying to? Delete the two float words in the powf() function.
     
  20. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #20
    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
     
  21. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #21
    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.
     
  22. thread starter macrumors 65832

    benlangdon

    Joined:
    Jan 13, 2008
    #22
    [​IMG]
    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
     
  23. macrumors regular

    Joined:
    Jan 17, 2007
    #23
    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.
     
  24. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #24
    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.
     
  25. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #25
    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.
     

Share This Page