What do i use to exponetiate something?

This is a discussion on What do i use to exponetiate something? within the C++ Programming forums, part of the General Programming Boards category; I'm working on a program that I need to use this equation: 1/2^n where n is a for loop number ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Thumbs down What do i use to exponetiate something?

    I'm working on a program that I need to use this equation:
    1/2^n where n is a for loop number going up to 8. what can i use instead of carrot (^) to square or exponetiate somthing?

    Plz help,
    Artanis

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    std::pow(double, double);

    from <cmath>

    i.e.

    double result = std::pow(.5, n);
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    thanks

    thanks, how could i have been so stupid, heh heh

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you could also create you're own recursive function, but that isn't necessary... but i feel like it and i'm bored, so...

    Code:
    #include <iostream>
    using namespace std;
    
    double power(double,double);
    
    int main()
    {
         double number;
         double exp;
    
         ...
         
         cout<<"Answer: "<<power(number,exp);
    
         return 0;
    }
    
    double power(double number, double exp)
    {
         if(exp>1)
              return number*power(number, exp-1);
         else
              return number;
    }
    there it is... although it only works for positive exponents...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    in your power function you need to add...
    Code:
    else if(exp==0)
    return 1;

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's why i said positive ;-)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    oops i didnt see that sorry

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Use the force... uh... I mean the library.

    Although writing your own functions is the best exercise, and there's nothing more fun than creating a recursive function The built-in library functions are almost always better (bug-free, fool-proof, flexable, etc.)

    And, a "simple" recursive function can't do this: pow(0.5, 2.4)... In fact, I don't know how to do that! Hmmm, I'd have to dig out my math books... must have something to do with summing exponents...

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Re: Use the force... uh... I mean the library.

    Originally posted by DougDbug
    And, a "simple" recursive function can't do this: pow(0.5, 2.4)... In fact, I don't know how to do that! Hmmm, I'd have to dig out my math books... must have something to do with summing exponents...
    Raise 0.5 to the 24th power, and take the 10th root (or the 12th power and take the 5th root, etc.). Use your home-built recursive function for the raising, but alas the only way I ever figured out how to get roots (other than the square root) is std::pow...

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well recursive functions are fun to write... kinda like linked lists... or am i just strange?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I wonder how difficult it would be to make our own exp function with inline assembly...Arn't their ASM commands to do logarithms? (I don't have any x86 reference manuals, otherwise I would look in those)
    "What are you after - the vague post of the week award?" - Salem
    IPv6 Ready.
    Travel the world, meet interesting people...kill them.
    Trying to fix or change something, only guaruntees and perpetuates its existence.
    I don't know about angels, but it is fear that gives men wings.
    The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak.

    E-Mail Xei

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    3

    Talking

    Wow, you all went so far off from my question... Though these inquires are intriguing. I say thanks to Zach L. I didn't have time to make a recursive function, though it almost came to that!

Popular pages Recent additions subscribe to a feed

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