how to use exponents

This is a discussion on how to use exponents within the C++ Programming forums, part of the General Programming Boards category; what do i use to program exponents in c++ usually its the carrot symbol "^" i tried that and it ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    4

    how to use exponents

    what do i use to program exponents in c++ usually its the carrot symbol "^" i tried that and it doesnt work i was just wondering what that command is in c++

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    there is no command, try using cmath aka math.h.

    pow()
    i think there is an exp() fxn.

    that or you could write your own fxn.

  3. #3
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    write your own func!

    Code:
    #include <iostream>
    using namespace std;
    
    namespace MATH
    {
    	template<class type>
    	type pow(type x, int exp)
    	{
    		for(int i = 1; i < exp; i ++, x *= x);
    		return x;
    	}
    }
    
    int main()
    {
    	cout << MATH::pow(2, 2) << endl;
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    yeah I would write my own function too, because the library functions tend to be inefficient ( they are code-heavy because they take into account and handle all sorts of situations that may not even be possible in your situation ).

    Code:
    int power( int number , int exponent ){
          return ( exponent == 2 ) ? number * number : number * power( number , exponent - 1 ); 
    }

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    thanks

    but for some reason i cant seem to write a function for non "int" numbers i need a function that can handle floating point numbers also such as .5(square root) and like 1.87 anyway any input on this would be very helpfull i just started studying c++ last night

    -matt

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's an example of using the one in the library.
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
       double a;
    
       a = pow(2.,10.);
       cout << a << endl;
    
       return 0;
    }

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    When you declare (create) a variable, you have to name the TYPE (char, int, double, float, ect). Look-up "variables" and "types"

    Next, look-up how to use cin and cout to input and display numbers in various formats.

    In general, when you combine two different types in an equation, the result is "promoted" to the longer/more complex type. If you multiply a float by an int, you get a float. But, you will get an error if you try to assign a float value to an int.

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by DougDbug
    But, you will get an error if you try to assign a float value to an int.
    you won't get an error, you'll get a warning. the program will still run, the value will just be truncated to become an int.
    warning for both initilization and assignment.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    abrege, your function is erroneous.
    Change the function body into this to make it work:
    Code:
    type value=x;
    for(int i = 1; i < exp; i ++, value *= x);
    return value;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Doesn't that have the same effect?
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Try it!
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    4
    thanks swoopy that did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exponents are LAAAME
    By Sm33zy in forum C Programming
    Replies: 8
    Last Post: 11-21-2008, 09:10 PM
  2. Fraction Exponents
    By ninjaturtle[k9] in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2004, 10:46 AM
  3. non-interger exponents
    By brane_sail in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2003, 12:41 AM
  4. God, i feel like a n00b again. How do i do exponents?
    By Inquirer in forum C++ Programming
    Replies: 13
    Last Post: 09-01-2003, 08:41 PM
  5. For loop and exponents
    By TrazPFloyd in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2002, 05:19 AM

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