LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-30-2004, 10:33 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
Lightbulb c math calculation


Hi i want to calculate the following function

T=-(1/l)*ln(1-y)...

I must do this without using an ln function...but only using the log...
Any good suggestion?
Thx
 
Old 05-30-2004, 11:37 AM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
You know, of course, that ln(a) == log_e(a). Remember that one of the properties of logarithms is log_b(a) == log_n(a)/log_n(b). So, ln(a) = log(a)/log(e). Here's a little C sample to show that the two are equivalent:

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

int main( int argc , char* argv[] ) {

   double a = 123.45;
   double e = exp(1.0);
   double maxDelta = 0.000000000000001;
   double delta = 1.0;

   delta = fabs( log(a) - log10(a)/log10(e) );

   if( delta < maxDelta ) {
      printf( "log_b(a) == log_n(a)/log_n(b)\n" );
   }
   else {
      printf( "log_b(a) != log_n(a)/log_n(b)\n" );
   }

   return( 0 );
}
When you compile you have to link in the math library:
Code:
gcc -o foo foo.c -lm

Last edited by eric.r.turner; 05-30-2004 at 11:43 AM.
 
Old 05-31-2004, 07:06 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
thx a lot

Do u know if the ln function exists?
I 've tried the ln(y) but it didn't work...


Now as far as your program is concerned i would say

that it doesn't seem to be working

T = (float) rand()/RAND_MAX;
//ln(a) = log(a)/log(e)
printf("*************T = %f \n",T);
temp=log(1-T) - log10(1-T)/log10(e);
T=-(1/l)*temp;

I want to calculate the T=1(1/l)*ln(1-y)

Are u sure that the e must be declared as double... I think that float is much more proper....

Thx a lot
 
Old 06-01-2004, 02:46 AM   #4
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
In math.h, the natural log function (ln) is really log(). Read the man page for log().

You want double for e. Double has greater precision than float, and the exp() function returns a double. Read the man page for exp().

My little program only shows you how to calculate ln() without using the math.h log() function. Instead you use log10(), which is log base 10. The rest is up to you.

Before you use a function you are not familiar with, you really must read the documentation for that function and make sure you are using the correct argument types and return types!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clients BandWidth Calculation ahmad82pkn Linux - Networking 0 10-22-2005 09:38 AM
Help needed regarding bandwidth calculation russoue Programming 2 07-13-2005 10:51 AM
improve math calculation... java os2 Programming 1 10-21-2004 07:17 PM
putting a math calculation in a cron job the_rhino Linux - Newbie 3 10-11-2004 02:46 PM
Load average calculation ? mikeshn Linux - General 4 08-27-2003 03:53 PM


All times are GMT -5. The time now is 12:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration