arithmetic in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting arithmetic in ksh
# 8  
Old 01-30-2007
Quote:
Originally Posted by d.f.
Isn't this the "preferred" way of doing arithmetic in ksh and bash? I recall reading that it doesn't spawn a new shell like expr and bc.
Yes,
but for floating point arithmetic you need bc or awk.
# 9  
Old 02-03-2007
Just for giggles, here is a way of doing integer division with rounding resulting in a floating point quotient.

Code:
#!/bin/ksh

    typeset -Z3     PRC=0 DEC=0
    integer         INT=0

    integer         CATCH=14
    integer         NUM=100


    (( INT = ( ( ( CATCH * 1${PRC}0 ) / NUM ) + 5 ) / 10 ))

    (( DEC = $INT % 1${PRC} ))
    (( INT /= 1$PRC ))

    echo "$CATCH / $NUM = $INT.$DEC"

which produces
14 / 100 = 0.140

The precision is controlled by the number of leading zeros in the typeset -Z# line. It is cumbersome but preserves leading zeros in the decimal portion.

Last edited by hegemaro; 02-09-2007 at 10:35 AM.. Reason: correct alorithm
# 10  
Old 02-03-2007
Also, most version of unix supply ksh88. But with ksh95, you can do floating point arithimetic in ksh. "float" is alias for "typeset -E" (which actually uses the C language double precision arithmetic). I hope the various vendors will upgrade to ksh95 someday.
# 11  
Old 02-04-2007
Quote:
Originally Posted by Perderabo
Also, most version of unix supply ksh88. But with ksh95, you can do floating point arithimetic in ksh. "float" is alias for "typeset -E" (which actually uses the C language double precision arithmetic). I hope the various vendors will upgrade to ksh95 someday.
I think you mean ksh93.
# 12  
Old 02-04-2007
Opps...ksh93 it is... good catch.
# 13  
Old 02-05-2007
Guys..

Thanks for this wonderful explanation..I think this will be useful post!

Cheers,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Arithmetic with bash

I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using: tr -cd “”¯ < afile | wc -c I am also able to get the total number of characters using: wc -c afile How do I divide the first... (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

Need help with date arithmetic please

Hello fellow forum members, I wrote below piece of code to calculate the date after a given date - date=$DATE_FINAL declare -a max_month=(0 31 28 31 30 31 30 31 31 30 31 30 31) eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100... (9 Replies)
Discussion started by: ektubbe
9 Replies

3. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

4. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

5. Shell Programming and Scripting

Arithmetic operations in bash,ksh,sh

Guys, The below expression is valid in which shells (sh,ksh,bash,csh)? VAR1=2 VAR2=$(($VAR1 -2)) Thanks (1 Reply)
Discussion started by: rprajendran
1 Replies

6. UNIX for Dummies Questions & Answers

Arithmetic: how to??

Hello all, I'd like to know how to perform arithmetic on multiple files. I have got many tab-delimited files. Each file contains about 2000 rows and 2000 columns. What I want to do is to to sum the values in each row & column in every file. The following explains what I want to do; ... (9 Replies)
Discussion started by: Muhammad Rahiz
9 Replies

7. Shell Programming and Scripting

ksh-script "arithmetic syntax error" comparing strings

Hi all, I´ve already searched the forum but can´t find what i am doing wrong. I am trying to compare two variables using ksh under red hat. The error I get is: -ksh: .: MDA=`md5sum /tmp/ftp_dir_after_transfer | cut -d' ' -f1 ` MDB=`md5sum /tmp/ftp_dir_before_transfer | cut -d' ' -f1 `... (3 Replies)
Discussion started by: old_mike
3 Replies

8. Shell Programming and Scripting

strange ksh arithmetic

Hello, I would like to understand this... I'm using ksh and doing (( z = y - 1 )) if y=34, then the result for z is 33, but if y=034 the result is z=27. Why?? Thanks (15 Replies)
Discussion started by: fbg
15 Replies

9. Shell Programming and Scripting

Help with arithmetic operation

I am using egrep to extract numbers from a file and storing them as variables in a script. But I am not able to do any arithmetic operations on the variables using "expr" because it stores them as char and not integers. Here is my code and the error I get. Any help will be appreciated. #!/bin/sh... (3 Replies)
Discussion started by: emjayshaikh
3 Replies

10. UNIX for Dummies Questions & Answers

Arithmetic In UNIX

I am a beginner and I have been searching the web all morning for this answer but can't find it anywhere. I know that prtint 1*2 will return 1*2 what if i actually want the problem to be calculated so i want print 1*2 to return 2 How is this done? (3 Replies)
Discussion started by: tygun
3 Replies
Login or Register to Ask a Question