Thread: math libs?

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    26

    math libs?

    I need to implement some math functions without depending on a C library (such as exp, pow and log).. Where can I find algorithms or even code?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    As long as you know how to perform these calculations without a calculator (can you believe a lot of us had to do this stuff by hand?!) then you should be able to figure it out pretty easily. I'll just give you one to get you started:
    Code:
    #include <stdio.h>
    
    double mypow(double x, double y)
    {
      double rv = 1.;
    
      while(y-- > 0)
        rv *= x;
    
      return rv;
    }
    
    int main(void)
    {
      int x = 2, y = 4;
    
      printf("%d\n", (int)mypow(x, y));
    
      return 0;
    }
    It doesn't handle negative exponents correctly, but you get the idea.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    works fine for ints, but how about floating point? And how do you do logarithms by hand? (I only know to use log tables)
    Last edited by ZeroG; 08-04-2004 at 12:06 AM.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You can do many things using series approximations. e.g.
    Code:
    ln((1+x)/(1-x)) = 2x + (2/3)x^3 + (2/5)x^5 + (2/7)x^7 + ...     for |x| < 1
    clearly there's a bit of work to be done getting everything into the right form
    and working out how many terms to calculate but its not too hard.

    Powers can be calculated using logs. eg:
    Code:
    x^y = e^(y ln(x))
    e^x = 1 + x + (1/2)x^2 + (1/3!)x^3 + (1/4!)x^4 + ...
    for more series take a look at http://mathworld.wolfram.com/MaclaurinSeries.html
    Note: there may be quicker formulations that you can use, but I would get it working
    first and understand the theory behind it. You can worry about speed-ups later!
    DavT
    -----------------------------------------------

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    thanks.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can also look at the source for libm to see how they do it. As long as you give proper credit in your functions you can use their code.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    ..or you can use FPU trough inline assembly. Here's tutorial: http://masmforum.com/website/tutoria...ute/index.html (some asm knowledge required)

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    26
    thanks all for links and ideas.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  2. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM