Error in mathematical calculation using bc


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error in mathematical calculation using bc
# 1  
Old 12-23-2009
Error in mathematical calculation using bc

I am getting the error:
Code:
Runtime error (func=(main), adr=10): Divide by zero

When executing the mathematical expression:
Code:
echo "scale=2; 1-(0/0)"|bc

How to overcome this?

Last edited by proactiveaditya; 12-23-2009 at 07:48 AM..
# 2  
Old 12-23-2009
What is (0/0) supposed to mean?

You can't divide by zero!
# 3  
Old 12-23-2009
I need to perform a mathematical operation:
Code:
1-($a/$b)

,where a and b can be any integer or floating point number and sometimes a=b=0.
# 4  
Old 12-23-2009
Hi proactiveaditya, you can test if b=0 and if it is then either set a and b to your own fantasy values or do not use bc in that case. Also use scale instead of scala.
# 5  
Old 12-23-2009
Well, then you'll have to do the same thing as in any programming language: catch the boundary case where $b is zero, and throw an error back to the user before bc can.
# 6  
Old 12-23-2009
@Scrutinizer
scala,thats a typo. Thanks for pointing out.

---------- Post updated at 06:54 AM ---------- Previous update was at 06:52 AM ----------

Got your point guys.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mathematical Operations on Column

Hi All, I want to perform a mathematical operation on column. Can anyone please help? Here is the sample of operation to be performed: 123 996 100 123 996 200 123 996 200 2015-09-21 123 996 100 123 996 200 123 996 100 What I want is to multiple all values of column # 3 by 100 and... (3 Replies)
Discussion started by: Zaib
3 Replies

2. Shell Programming and Scripting

Mathematical calculations using shell

Dear All, I read some variables in a file and assigned as name for each of them. If I do echo I am able to see the values as 1.0E-05,3.4,5.0E-03 etc, Now I want to do some mathematical operations with them. Lets say 1 1.0E-05*5.0E-03 expected ans is 5.0E-08 2 1.0E-05/5.0E-03 expected... (9 Replies)
Discussion started by: linuxUser_
9 Replies

3. Shell Programming and Scripting

Translate bash mathematical calculation to awk

this code below is very useful in calculating mean and quartiles. however, i would really like to translate it to awk without having to write to any external file: #!/bin/sh filename="tmp.txt" sort -n $1 >$filename rows=`wc -l $filename|cut -d' ' -f1` q2=`echo "($rows+1)/2" |bc` ... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Homework & Coursework Questions

Help mathematical shell programming

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

5. Homework & Coursework Questions

Mathematical scripting question

Hello Guys,For my homework I must write a shell script to do this serie, http://upload.wikimedia.org/math/f/8/f/f8f543d9ecd01c4ecca2a0b7bc1234a2.pngI know that I must use the "bc" for that, but for the script's itself i have no idea,(beginner) Can you plz just help me for have some idea?... (1 Reply)
Discussion started by: hamed.samie
1 Replies

6. Shell Programming and Scripting

Mathematical calculations in shellscript

Hi want to do below mathematical calculations in shellscrip but it is not giving me the exact output.Please help me to solve this price=95.3 price1=(20/100)*$price+$price echo "price=$price1" finally the output should display price=114.36 Thanks (5 Replies)
Discussion started by: aish11
5 Replies

7. Shell Programming and Scripting

Mathematical Loop

Hi, I'm creating a loop that allows the user to enter any number, then their choice of operator and then another number until their operator choice is equal to = But I am getting an error saying integer expression expected. Any explanation on why this is happening? echo "Please enter a number"... (1 Reply)
Discussion started by: Addman1991
1 Replies

8. Shell Programming and Scripting

Minor Calculation Error

Hello everyone!! I got a slight problem doing some calculation from the text file. I able to get the specific data by cutting it using grep and cut. The amount can be calculated but the problem I faced now is even the field which I didnt cut is been calculated too. This is what I meant. The... (2 Replies)
Discussion started by: aLHaNz
2 Replies

9. Shell Programming and Scripting

Mathematical functions in bash shell

Hi, How can i do the mathematical calculations in bash shell? Are the mathematical functions available in bash shell? Ex: pow ceil floor sqrt (5 Replies)
Discussion started by: cola
5 Replies

10. Shell Programming and Scripting

Avoid "++ requires lvalue" Error in Loop Calculation

Hi All, Please help me to perform sum of values in a loop, I am getting following error: "total=0++432907765772: ++ requires lvalue" where actual statement is as : total=$total+$amt where amt can have +ve or -ve values Thanks Sandeepb (3 Replies)
Discussion started by: sandeepb
3 Replies
Login or Register to Ask a Question