math in unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers math in unix
# 8  
Old 03-03-2009
use expr instead
if u have variables a and b
c=` expr $a+$b `
# 9  
Old 03-03-2009
Quote:
Originally Posted by talk2hod
These methods are very good, but when you are going for float addition i.e. 1.3+3.4 then it won't work.
Code:
>echo 1.3 3.4 | awk '{print $1 + $2}'
4.7
>echo 1.3 3.4 | awk '{print $1 / $2}'
0.382353
>echo 1.3 3.4 | awk '{print $1 * $2}'
4.42
>echo 1.3 3.4 | awk '{print $1 - $2}'
-2.1

The awk method, at least, should work perfectly well.
# 10  
Old 03-03-2009
#!/bin/ksh
a=1.3
b=3.4
c=`echo $a + $b | bc`
echo "The value is $c"
exit 0

Try this....
# 11  
Old 03-03-2009
Code:
[rick@kangaroo ~]$ a=1.3
[rick@kangaroo ~]$ b=5.1
[rick@kangaroo ~]$ c=$(echo $a + $b | bc)
[rick@kangaroo ~]$ echo $c
6.4
[rick@kangaroo ~]$

# 12  
Old 03-03-2009
a=2
b=3
c='echo $a+$b | bc'
echo $c


# two variables with operator + in between are echoed as a string
and passed on to bc to evaluate, which is stored in variable c.
# 13  
Old 03-04-2009
a=2
b=9
c='echo $a+$b | bc'
echo $c


this should work
# 14  
Old 03-04-2009
The best way to use maths function is by "bc" command. It takes care of integer and float both.
Usage: echo the maths expression and pipe it to bc.
add:
===
$ echo "2+2" | bc
$ 4
$ echo "2.5+4.8" | bc
$ 7.3

multiply:
======
$ echo "2.5*4.6" | bc
$ 11.5

Getting Powers:
============
$ echo "2 ^ 6" | bc
$ 64

You can define the precision scale i.e. the number of digits you want after decimal by using scale command.

For Example, for division
$ echo "scale=9; 22 / 7" | bc
3.142857142

or for square root:
$ echo "scale=5;sqrt ( 2 )" | bc
$ 1.41421


There are many more function in bc.....
s(x): Sine
c(x): Cosine
e(x): Exponential
l(x): Log
a(x): arctangent
..
..
..

Hope this helps!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Math

i have file (my_file.txt) that looks like this: 000000000000010000 000000000000010000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 000000000000005000 all said and one, it should look... (11 Replies)
Discussion started by: lawsongeek
11 Replies

2. UNIX for Dummies Questions & Answers

Linux Math Help

I am struggling with scripting this challenge a friend and I have. You have file1 and its contents is a single number you have file 2 and its contents are a different number you want to add file1 to file2 and have the output be put into file3 (3 Replies)
Discussion started by: minkyboodle
3 Replies

3. Shell Programming and Scripting

Math Tool

Hi all, I am new to PERL scripts, and i have made my first script which i am posting here. This math tool performs all basic arithmatic functions. #!/usr/bin/perl print "\t----------Welcome to Maths Tool-----------\n"; do { print "Enter your choice :"; print... (2 Replies)
Discussion started by: PranavEcstasy
2 Replies

4. Shell Programming and Scripting

Need help with AWK math

I am trying to do some math, so that I can compare the average of six numbers to a variable. Here is what it looks like (note that when I divide really big numbers, it isn't a real number): $ tail -n 6 named.stats | awk -F\, '{print$1}' 1141804 1140566 1139429 1134210 1084682 895045... (3 Replies)
Discussion started by: brianjb
3 Replies

5. Homework & Coursework Questions

Unix/Linux Math Decimal to Whole Number Format?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem? I hope I fill this out correctly. I have a program that runs like a cash register. It works and... (6 Replies)
Discussion started by: Iceman69
6 Replies

6. Shell Programming and Scripting

math help

$ x=1 $ y=1.5 $ z=$((x*y)) bash: 1.5: syntax error: invalid arithmetic operator (error token is ".5") What's wrong? (2 Replies)
Discussion started by: rockbike
2 Replies

7. Programming

some math problems in C

I want to calculate secant method using C language That is a program----> #include<stdio.h> #include<math.h> #include<stdlib.h> main() { double fx(double x); double x0,x1,x2,f0,f1,f2,err; int n,i; printf("\n\n f(x) =x*x*x-5*x-7"); printf("\n\nEnter an interval in" ... (4 Replies)
Discussion started by: cdfd123
4 Replies

8. Programming

something about <math.h>

Hi, I got an easy problem for you but really difficult for me 'cause I am pretty new to this field I got header file <math.h> included in my .c file , then I write the code as below: k = sqrt(i); /* both variables k and i are int */ then I cc temp.c it says like this undefined... (4 Replies)
Discussion started by: blf0
4 Replies

9. Shell Programming and Scripting

Math calculation help

Hi, I wrote this script awk -F"\t" '{if ((($1 == 586) || ($1 == 68030)) && (($2/1024) < 512)) print $0"\t"(512-($2/1024))"\t"(512-($2/1024))/256}' pcs.txt But I want from the calculation in red to get rid of the decimal part. Like instead of 1.75 to keep only 1.Please somebody tell me what... (4 Replies)
Discussion started by: sickboy
4 Replies

10. Programming

math.h not working? o.0

Alright, umm i cant get this to work. im looking at some example and a book i have. when i try to compile my program i get an error message. ld: 0711-317 ERROR: Undefined symbol: .sqrt ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information. I did #include<math.h> after my... (2 Replies)
Discussion started by: primal
2 Replies
Login or Register to Ask a Question