Like Tree2Likes
  • 1 Post By AndrewHunter
  • 1 Post By hk_mp5kpdw

Using an Exponent in C++

This is a discussion on Using an Exponent in C++ within the C++ Programming forums, part of the General Programming Boards category; Hey guys I am still really new to C++, as I am currently taking my first programming class. I am ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    Using an Exponent in C++

    Hey guys I am still really new to C++, as I am currently taking my first programming class. I am currently working on a payment calculator. I am using Visual Studio 2010. Before I get into this I want to say this is NOT homework just simply practice for me. This is what I have so far.

    #include<iostream>
    #include<math.h>
    usingnamespace std;
    intmain()
    {

    float rate, priceOfHouse, years, finalPayment;

    const double MONTHLY_INTEREST_RATE = (rate/100/12);

    const float INTIAL_MONTHLY_PAYMENT = (rate/100/12) * priceOfHouse;

    const double LENGTH_OF_LOAN = -years/12;
    cout <<
    "Enter Interest Rate: ";
    cin >> rate;
    cout <<
    "Enter Price of House: ";
    cin >> priceOfHouse;
    cout <<
    "Length of Loan in terms of Years: ";
    cin >> years;
    finalPayment = INTIAL_MONTHLY_PAYMENT / (1 - ((1 + (MONTHLY_INTEREST_RATE)) exp(LENGTH_OF_LOAN)))

    ************************************************** ******
    The problem that I am stuck at right now is this last line finalPayment =, the exponent (exp) part. It is telling me Error: expected a ")"
    With the variables plugged in the formula is suppose to look like so: (input numbers are just expamples)
    ((6.5/100/12)*200000 / (1-((1+(6.5/100/12))^(-30/12)))

    Not looking for anyone to finish this for me just a little help with this last line, maybe an example of what something like this (a formula) would look like in C++

    Thanks,
    NismoT



  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    ((6.5/100/12)*200000 / (1-((1+(6.5/100/12))^(-30/12))))
    ==
    Code:
    ((6.5/100/12)*200000 / (1-pow(((1+(6.5/100/12)),(-30/12)))))
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Elysia likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    You seem to think that
    a^b = a exp(b)
    which is not the case.
    In C++, a^b = exp(a,b)

    Take that into account when writing your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,539
    Quote Originally Posted by Elysia View Post
    You seem to think that
    a^b = a exp(b)
    which is not the case.
    In C++, a^b = exp(a,b)
    It is not that either.

    In C++ (and C) exp() is a function that accepts one argument. exp(a) computes e to the power of a, where e is the base for natural logarithms (2.718....)

    If you want to compute a to the power of b, where both a and b are of type double, the form is pow(a, b). Bear in mind that some forms of raising a value to a power are trivial. In practice, it is often preferable to perform a*a rather than pow(a, 2.0), for example.

    And a^b in C++ is a bitwise operation on integral values (which is why I've used words "to the power of" rather than a^b notation).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Right logic, wrong function, doh >_<
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,539
    Yup.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    float rate, priceOfHouse, years, finalPayment;
    const double MONTHLY_INTEREST_RATE = (rate/100/12);
    const float INTIAL_MONTHLY_PAYMENT = (rate/100/12) * priceOfHouse;
    const double LENGTH_OF_LOAN = -years/12;
    
    cout << "Enter Interest Rate: ";
    cin >> rate;
    cout << "Enter Price of House: ";
    cin >> priceOfHouse;
    cout << "Length of Loan in terms of Years: ";
    cin >> years;
    There is a problem with the order of these operations. At the point where you are initializing your constants, the variables rate/years/priceofHouse will have a random value. This random value will be used to initialize your constants. Later, reading into these variables does not magically change the values of the previously initialized constants based on the user's input. You need to get the user's input first and then calculate those constants.

    Code:
    #include <math.h>
    You should probably use this instead:
    Code:
    #include <cmath>
    ...not that it's likely to matter much.
    iMalc likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does anyone know how to do exponent with just addition?
    By cowboyz209 in forum C Programming
    Replies: 11
    Last Post: 05-03-2011, 03:26 PM
  2. mantissa, sign, exponent
    By -EquinoX- in forum C Programming
    Replies: 28
    Last Post: 03-05-2008, 02:25 PM
  3. Some Exponent Help
    By jrahhali in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-04-2005, 08:31 PM
  4. Exponent help
    By flatline911 in forum C++ Programming
    Replies: 4
    Last Post: 08-15-2003, 02:34 AM
  5. exponent
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 04-14-2003, 03:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21