How to insert natural logarithm in C.Urgent...

Discussion in 'Mac Programming' started by gusious, Jun 25, 2008.

  1. macrumors 65816

    gusious

    Joined:
    Dec 2, 2007
    Location:
    Greece
    #1
    Hi.This is a bit urgent....

    I am writing a program in C and i need to insert ln(natural logarithm).How do i do that?
     
  2. macrumors member

    Joined:
    Apr 8, 2008
    #2
    #include <math.h>

    log(number); // Computes the ln of number


    Is that what you need?
     
  3. thread starter macrumors 65816

    gusious

    Joined:
    Dec 2, 2007
    Location:
    Greece
    #3
    That's what i have inserted but i have this message: error: invalid lvalue in assignment

    What's that?Oh,should i declair the value ln(number) which is ln(pi)+=log(pi) as float?
     
  4. macrumors member

    Joined:
    Apr 8, 2008
    #4
    I think your problem is that ln(pi) is not a variable; thus, you can't use it on the left side of an assignment.

    I think what you want is:

    double result;
    result = log(pi)+log10(pi); //assigns the result of ln(pi) plus log(base ten)(pi) to result.
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    You use log(x) to calculate the natural logorithm of x. C is like mathematicians, who use log to mean the natural log rather than ln.

    see man math on the command line for this information.
     
  6. thread starter macrumors 65816

    gusious

    Joined:
    Dec 2, 2007
    Location:
    Greece
    #6
    No i don't need log.I only need ln.Wait a sec.Or more.I had an idea...

    Check this out:

    #include <stdio.h>
    #include <math.h>

    main()
    {
    float H,pi;
    int n,ni,i,x[1000],y[1000],points;


    points=0;
    printf("insert ni and n\n");
    while(scanf(" %d %d ",&ni,&n) ==2)
    {
    x[points]=ni;
    y[points]=n;
    points++; }
    pi=0;
    H=0;
    for(i=0; i<points; i++)
    {
    pi= pi + x/y;
    }

    H=-(pi*ln(pi));
    printf("The pi is %f,the ln(pi) is %f and the H is %f\n",pi,ln(pi),H);
    }

    I want to calculate the sum pi*ln(pi).So,can you find me the problem?
     
  7. Nuc
    macrumors 6502a

    Nuc

    Joined:
    Jan 20, 2003
    Location:
    TN
    #7
    Listen to Eraserhead

    Change ln(...) to log(...) like he said. If you're not satisfied with his answer then try it and then double check it with your calculator...

    Nuc
     
  8. macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #8
    Change 'ln' to 'log'.

    In programming log(N) is the natural log of N.

    Of course the program will never end unless you error it somehow. Glad I never wasted time "learning" programming in school. I wonder how anyone gets anywhere at first.
     
  9. thread starter macrumors 65816

    gusious

    Joined:
    Dec 2, 2007
    Location:
    Greece
  10. macrumors 6502

    Joined:
    Apr 28, 2008
    #10


    I see you've got it now...
     

Share This Page