LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2010, 12:03 AM   #1
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Rep: Reputation: 17
Unhappy cannot add fractions in loop using bc in a script?


hi dear all

i have tried a lot to add floating values in while loop using bc, but no sucess!!!


script is :

read a1
read a11

b=$(echo "scale=10; $a1/1000" | bc)
c=$b
echo "c is $c"

while [[ $a1 -lt $a11 ]] ; do
c=$(echo "scale=10; $c+0.005" | bc) #
echo "c is $c"
done

output:
....
....
c is 12.7000000000
c is 12.7050000000
...
.....infinite loop?

with regards!!!
skumar
 
Old 02-11-2010, 12:42 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
$a1 and $a11 are not changed in the loop so the [[ $a1 -lt $a11 ]] test will always return the same result.
 
1 members found this post helpful.
Old 02-11-2010, 12:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use [code][/code] tags around your code, to preserve formatting and readability.

Second, your while loop doesn't do anything to change the test.
Code:
while [[ $a1 -lt $a11 ]] ; do

     c=$(echo "scale=10; $c+0.005" | bc)

     echo "c is $c"

done
If a1 is less than a11, the loop will continue indefinitely.

Last edited by David the H.; 02-11-2010 at 12:43 AM.
 
1 members found this post helpful.
Old 02-11-2010, 01:08 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Your problem is that bash only does integer comparisons (even though bc can do float calcs); the usual trick for langs like that is simply to multiply (scale) your actual values up enough to be able to treat them as integer eg if you want increments of 0.1, multiply by 10 and use increments of 1.
You could just use a better lang eg Perl.
 
1 members found this post helpful.
Old 02-11-2010, 07:42 AM   #5
sonu kumar
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: open suse
Posts: 28

Original Poster
Rep: Reputation: 17
Unhappy

Quote:
Originally Posted by chrism01 View Post
Your problem is that bash only does integer comparisons (even though bc can do float calcs); the usual trick for langs like that is simply to multiply (scale) your actual values up enough to be able to treat them as integer eg if you want increments of 0.1, multiply by 10 and use increments of 1.
You could just use a better lang eg Perl.
hi chris !!

can you tell/suggest me the good and simple links to learn the perl language.

thanks in advance!!!
Sk

Last edited by sonu kumar; 02-11-2010 at 08:56 AM. Reason: edit to add
 
Old 02-11-2010, 05:24 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://perldoc.perl.org/ - lang definition with loads of examples & extra tutorials
http://www.perlmonks.org/?node=Tutorials - more tutorials

Enjoy
 
1 members found this post helpful.
  


Reply



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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
bc and exponents containing decimals and fractions. Dogs Programming 6 03-12-2013 04:27 PM
How to loop a script? rollo Linux - General 3 12-21-2006 05:39 AM
how to convert binary fractions to decimal jabka Programming 12 12-16-2006 06:05 PM
how to work with fractions in c ? (help neded); jabka Programming 7 11-20-2006 03:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 PM.

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
Open Source Consulting | Domain Registration