PDA

View Full Version : How to insert natural logarithm in C.Urgent...

gusious
Jun 25, 2008, 03:36 PM
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?

luckylefty01
Jun 25, 2008, 03:41 PM
#include <math.h>

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

Is that what you need?

gusious
Jun 25, 2008, 03:50 PM
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?

luckylefty01
Jun 25, 2008, 03:57 PM
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.

Jun 25, 2008, 03:59 PM
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.

gusious
Jun 25, 2008, 04:09 PM
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[i]/y[i];
}

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?

Nuc
Jun 25, 2008, 04:16 PM
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

Sayer
Jun 25, 2008, 04:18 PM
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.

gusious
Jun 25, 2008, 04:18 PM
Yup.I just saw that and correct it.

Enuratique
Jun 25, 2008, 04:22 PM
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[i]/y[i];
}

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?

I see you've got it now...