LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 01: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, 01:42 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
$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, 01:42 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 01:43 AM.
 
1 members found this post helpful.
Old 02-11-2010, 02:08 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 08: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 09:56 AM. Reason: edit to add
 
Old 02-11-2010, 06:24 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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


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


All times are GMT -5. The time now is 10:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration