Calculation with floats


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculation with floats
# 1  
Old 11-11-2010
Calculation with floats

I want to make computations using floats. This cannot be done with csh. ksh seem to have a problem as well. What is good shell for computations without having to resort to bc or awk? How about python?
# 2  
Old 11-11-2010
So how about perl?
# 3  
Old 11-11-2010
This worked for me with ksh.

fa=12.123
fb=21.321
ans=$(($fa*$fb))

Are there any restrictions of floating point calculations in ksh?

---------- Post updated at 07:01 PM ---------- Previous update was at 07:00 PM ----------

I thought I move away from perl in favour of python. Any opinions?
# 4  
Old 11-11-2010
Float Manipulation with Perl

This will multiply two floats and print the result. You can modify the precision and padding by changing the FORMAT in the printf() statement. perldoc -f sprintf
Code:
printf "%f\n", $f1 * $f2;

Maybe that can tie you over until you get the desired answer in python.
# 5  
Old 11-11-2010
Quote:
Originally Posted by kristinu
This worked for me with ksh.

fa=12.123
fb=21.321
ans=$(($fa*$fb))

Are there any restrictions of floating point calculations in ksh?

---------- Post updated at 07:01 PM ---------- Previous update was at 07:00 PM ----------

I thought I move away from perl in favour of python. Any opinions?

Ksh dosn't work correctly for me:

Code:
$ echo $((2.9 * 1.9))
2

Also why not use bc it's much smaller than python/perl/awk?

Code:
$ size /usr/bin/bc /usr/bin/awk /usr/bin/perl
/usr/bin/bc: 13568 + 10344 + 41392 + 3876 = 69180
/usr/bin/awk: 86520 + 64696 + 419848 + 18802 = 589866
/usr/bin/perl: 1072227 + 45157 + 2941 + 122652 = 1242977

# 6  
Old 11-12-2010
Floating point arithmetic is fully part of ksh93, but not ksh88.

Code:
$ ksh --version
  version         sh (AT&T Research) 93t+ 2009-05-01
$ echo $((2.9 * 1.9))
5.51

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Extracting floats from a string in Python

I have this script which can calculate the total of numbers given in a string total = 0 for c in '0123456789': total += int(c) print total How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify... (2 Replies)
Discussion started by: faizlo
2 Replies

2. Shell Programming and Scripting

Calculation

Hi, i have a large file like this: Contig1 1 5 Contig1 2 4 Contig1 3 3 Contig1 4 5 Contig1 5 3 Contig1 6 4 Contig2 1 3 Contig2 2 7 Contig2 3 2 Contig2 4 9 Contig2 5 10 Contig2 6 3 Contig2 7 7 Contig2 8 2 Contig2 9 7 Contig2 10 5 contig1 2 4 contig1 3 3 contig1 4 5 (3 Replies)
Discussion started by: the_simpsons
3 Replies

3. Shell Programming and Scripting

VG calculation in GB

for i in `lsvg` do echo "VG Name:" $i echo "Total VG Size:" lsvg $i |grep "TOTAL PPs:" |awk '{print $7}' | cut -c2- echo "Free VG Size:" lsvg $i |grep "FREE PPs:" | awk '{print $7}' | cut -c2- done The PP Sizes are in MB. I like to have the sizes in GB. Also, I like to have the... (14 Replies)
Discussion started by: Daniel Gate
14 Replies

4. Shell Programming and Scripting

calculation

Could someone till me what this calculation really means let foo=`date "+(1%H-106)*60+1%M-100"` bar=foo+1440 (4 Replies)
Discussion started by: freddie999
4 Replies

5. Shell Programming and Scripting

integers, floats and text

I am using gawk in a dos shell in windows xp and want to read a datafile and reformat it. The datafile consists of columns of integers, floating point numbers and text strings. Each column is a fixed width and each column contains the same data type, eg all integers, all text. I am looking for a... (0 Replies)
Discussion started by: lookingfor help
0 Replies

6. Shell Programming and Scripting

problem with AWK and floats below 0

Hello. I have a problem with AWK and floats below 0 in a script. It may be simplified to this line (please, take into account that my "locale" is Spanish, so the system will read "," as decimal separator): echo -1,25 2,55745 0,33 ,278 | awk '{print $1+1, $2+1, $3+1, $4+1}'... getting: -0,25... (4 Replies)
Discussion started by: JCastro
4 Replies

7. Shell Programming and Scripting

summery calculation

Hi All I want to make summery for Date=245Duration=545 Date=245Duration=10 Date=245Duration=278 Date=246Duration=30 Date=246Duration=178 Date=246Duration=414 Date=247Duration=17 Date=247Duration=281 Date=247Duration=9 Date=248Duration=968 Date=248Duration=550 Date=248Duration=1011... (1 Reply)
Discussion started by: nalakaatslt
1 Replies

8. Shell Programming and Scripting

decimal calculation

Hi am calculating the percentage of the pass and fail. Pass: echo `echo 1498*100/1667| bc`% fail: echo `echo 169*100/1667 | bc`% for this am getting PASS= 89% fail =10 % I need to fetch the exact decimal percentage like PASS = 89.8 % FAIL= 10.2 % Please advice me (3 Replies)
Discussion started by: bobprabhu
3 Replies

9. Shell Programming and Scripting

calculation

Hi, I am in ksh88 I am trying to get the result of the calculation using 3 variables: TOTAL CAPACITY and get the following error: $DB_CAPACITY=(( $DB_SIZE * 100 / $TOTAL )) ksh: syntax error: `((' unexpected I cannot figure out what am I doing wrong... Thanks for any help -A (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies
Login or Register to Ask a Question