Implementation of the math.h libary

This is a discussion on Implementation of the math.h libary within the C Programming forums, part of the General Programming Boards category; Hello I would like to know how to implement the "ln" function in the math.h libary. I would like to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Implementation of the math.h libary

    Hello I would like to know how to implement the "ln" function in the math.h libary. I would like to implement the equation ln(a^2-d/2a) I would appriciate any help that anyone give me.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    double foo(double a, double d)
    {
       return log(exp(a * a) - d / (2 * a));
    }
    ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Having problems implementing

    Hello thanks for that but i'am still having problems implementing it. I want to implement the equation ln((8*L-1)/D) the code below is what i have that is not working

    Code:
    	b=log(exp((8*L)-1/ D));

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Learn about the change of base rule

    Quick tip: ((8*L-1)/D), as stated in your question, is not the same as ((8*L)-1/ D)
    which is in your code.

    Another quick tip: stick to one equation, as you have now posted 3 different
    equations.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All you need to know is basic math to figure out this one. * before / before + before -

    Thus, if you removed all of the parenthesis, and just relied on basic math properties, you would get:

    A = 8 * L resolved first.
    B = 1 / D resolved next.
    C = A - B resolved last.

    This is why we have the phrase, "When in doubt, use lots of parenthesis!" Or something to that effect.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Studying an operator precedence table might help: http://www.isthe.com/chongo/tech/com...recedence.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Still not working

    I corrected my mistake but the problem now is that the ln function which I wrote as log(exp) is not giving me a proper value I checked it

    b=log(exp((8*L-1)/D));

    if L=3
    D=7

    the answer is meant to be 1.189584 but instead it 3.285714

    I will be very greatful if someone can help me.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Do I have to do everything for you? I already posted a link for you to look
    at the change of base rule. By applying that, using log () - which is base 10,
    to get a result in ln you do this:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main (void)
    {
    	double b, L = 3, D = 7;
    	
    	b=(log((8*L-1)/D))/(log (exp (1.0)));
    
    	printf ("b is %f", b);
    
    	return 0;
    }
    Now go away.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Richie T
    log () - which is base 10
    Hmm?
    The log functions compute the base-e (natural) logarithm of x.
    The log10 functions compute the base-10 (common) logarithm of x.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Oops! My mistake, should have looked that up before I submitted, nonetheless
    the code I posted gives the right answer since it ends up dividing by 1!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 12:18 PM
  2. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 12:59 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 07:05 PM
  5. implementation?
    By calQlaterb in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:25 AM

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