LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-12-2008, 05:17 AM   #1
bluesmoke
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
calculate logarithm in bash script


Hi I want to calculate log in bash script. I searched but could not find any bash command to directly do it. Can any one give some idea to do it in simple way

Alternatively:
I am also thinking to send the variable in C program, calculate in C and get the answer back in bash script. But I think it is a long process. as I have to do this
i) gcc -o log log.c
ii) ./log > log_result.txt
iii) then read the result from log_result.txt

Can any body suggest a simple way. Thank You
 
Old 12-12-2008, 05:29 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Writing your own C code is like to create a new linux command. You can do the first step only once, then put the executable somewhere in your path and preserve it for future uses. Step 2 can be skipped as well if the C program send the output to stdout, so that you can do something like:
Code:
my_log=$(log 8)
but beware that bash does not support floating point numbers.

A ready alternative is to use some application which has the natural log function as a built in, like awk or bc:
Code:
my_log=$(echo "l(8)" | bc -l)
my_log=$(echo 8 | awk '{printf "%11.9f\n",log($1)}'
 
Old 12-12-2008, 05:29 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,414

Rep: Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967Reputation: 1967
http://www.phodd.net/cyrek/gnu-bc/bcfaq.php#bashlogh
 
Old 12-12-2008, 06:14 AM   #4
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
Or simply a for loop where you calculate the logarithm through it's Taylor's series around a point in the vicinity, which is how it's done anyway. OK, some algorithms use better series expansions. Check Wiki for some more.
 
Old 12-12-2008, 08:08 AM   #5
bluesmoke
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks to all of you for guiding.
http://www.phodd.net/cyrek/gnu-bc/bcfaq.php#bashlogh

@acid_kewpie : thanx..the link is usefull
 
Old 12-14-2008, 02:41 AM   #6
arunmathew1984
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Wow ... This thread was pretty informative. Nice work folks.


Linux Archive

Last edited by arunmathew1984; 12-20-2008 at 12:24 PM.
 
  


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
Perl/bash - calculate date in future rose_bud4201 Programming 6 05-10-2009 06:59 AM
bash calculate job duration Padawan.AVT Programming 2 10-07-2008 03:33 AM
Bash Script, calculate output. eldaria Programming 13 07-20-2006 10:26 PM
Calculate in shell script Ephracis Linux - General 1 12-16-2004 12:11 PM
How to calculate integer binary logarithm in C? shy Programming 1 11-27-2004 08:43 AM


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

Main Menu
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