Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org c math calculation
 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

 05-30-2004, 09:33 AM #1 alaios Senior Member   Registered: Jan 2003 Location: Aachen Distribution: Opensuse 11.2 (nice and steady) Posts: 2,197 Rep: 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
 05-30-2004, 10:37 AM #2 eric.r.turner Member   Registered: Aug 2003 Location: Planet Earth Distribution: Linux Mint Debian Edition (LMDE) Posts: 215 Rep: 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 #include 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 10:43 AM.
 05-31-2004, 06:06 AM #3 alaios Senior Member   Registered: Jan 2003 Location: Aachen Distribution: Opensuse 11.2 (nice and steady) Posts: 2,197 Original Poster Rep: 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
 06-01-2004, 01:46 AM #4 eric.r.turner Member   Registered: Aug 2003 Location: Planet Earth Distribution: Linux Mint Debian Edition (LMDE) Posts: 215 Rep: 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!

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post ahmad82pkn Linux - Networking 0 10-22-2005 08:38 AM russoue Programming 2 07-13-2005 09:51 AM os2 Programming 1 10-21-2004 06:17 PM the_rhino Linux - Newbie 3 10-11-2004 01:46 PM mikeshn Linux - General 4 08-27-2003 02:53 PM

LinuxQuestions.org

All times are GMT -5. The time now is 07:49 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -