Math Header?

This is a discussion on Math Header? within the C++ Programming forums, part of the General Programming Boards category; Couldn't you just write a short recursive function to handle "power of" math calls. Code: long PowerOf(long Base_Number, long Power_Of) ...

  1. #16
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Couldn't you just write a short recursive function to handle "power of" math calls.

    Code:
    long PowerOf(long Base_Number, long Power_Of)
    {
        long tmp=0;
        static long counter;
        while(Power_Of<=counter)
        {
            counter++;
            tmp=Base_Number*(counter);
            Power_Of--;
            tmp=PowerOf(tmp,Power_Of);;
        };            
        return tmp;
    }
    (I know this code has flaws)
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I needed it for a think called HEK
    Another .NET sellout. Did you know I couldn't even install my HP printer drivers on my older computer because it didn't have .NET installed?

    **EDIT**
    >>Couldn't you just write a short recursive function to handle "power of" math calls.
    a) Why rewrite the standard math library?
    b) How would you compute fractional powers by this method?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    Couldn't you just do a bit of code modification and change out the "long" parameters for "float"?

    Also, if you aren't interested in the bloat of including more headers files this should help. Or for the novelty, of I don't know, writing something yourself...

    I wasn't suggesting it was the be all/end all. Just an alternative. An idea.
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  4. #19
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    long PowerOf(long Base_Number, long Power_Of)
    {
    long tmp=0;
    static long counter;
    while(Power_Of<=counter)
    {
    counter++;
    tmp=Base_Number*(counter);
    Power_Of--;
    tmp=PowerOf(tmp,Power_Of);;
    };
    return tmp;
    }

    don't know what that would do...

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Couldn't you just do a bit of code modification and change out the "long" parameters for "float"?
    No. Because the power-ing is done by whole numbers. An interesting idea though, to do it with recursion... I would have just stuck it in a for loop
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    using recursion in the place of a simple for loop is not interesting, its slow and clumsy.
    hello, internet!

  7. #22
    Registered User BillBoeBaggins's Avatar
    Join Date
    Oct 2003
    Posts
    107
    You know I didn't even think of putting it in a loop...
    May the compiler be with you.
    Be one with the compiler, and you shall prosper greatly.

  8. #23
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Recursive power function:

    if 'base' is zero, then 'exp' must be positive
    Code:
    
    double power( double base, int exp)  
    {
          if (base == 0)  //base is zero, and exp should be positive
    
               return 0;
    
          else if (exp == 0)
    
               return 1;
    
          else if (n > 0)  
           
               return base * power(base, exp - 1);
    
          else //base is nonzero, and exp is negative
    
               return  1/power(base, -exp);
    
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #24
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    so can you explain what this does and how to use it if it will help me please?

  10. #25
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>so can you explain what this does and how to use it if it will help me please?
    It is a recursive function: It calls itself with changing parameters in order to accomplish the task. And it isn't useful to you at all, since <cmath> includes a pow() function
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #26
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    oh lol yes I have already used the pow function for my calculator.

  12. #27
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Cool

    Quote Originally Posted by Hunter2
    >>so can you explain what this does and how to use it if it will help me please?
    It is a recursive function: It calls itself with changing parameters in order to accomplish the task. And it isn't useful to you at all, since <cmath> includes a pow() function

    sometimes i get joy out of bludgeoning a fly with a hammer
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 03:49 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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