Operating System - HP-UX
1752328 Members
5660 Online
108786 Solutions
New Discussion юеВ

Re: Mathmatical term in the script

 
rayofmoon
Occasional Contributor

Mathmatical term in the script

how can I use mathmatical term in the script(like /,*,-,+) and also how can I round the fraction number in the script. pls anybody can help me
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: Mathmatical term in the script

You can use (( )) for arithmetic expressions on integral operations. If you need to do rounding, you'll need to do fixed point arithmetic.
Or use ksh93 (dtksh) or awk. See these two threads:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1151323
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1171007
James R. Ferguson
Acclaimed Contributor

Re: Mathmatical term in the script

Hi:

Script = ?

What language, or do you care? Shells, in general to integer arithmetic. However, the Korn93 (ksh93) shell supports real values:

# cat showme
#!/usr/dt/bin/dtksh
typeset -F2 R1
typeset -E R2
let R1=1/8+1
echo ${R1}
let R2=1/8+1
echo ${R2}

# ./showme
1.13
1.125

Notice that 'printf' supports rounding and can be used in 'awk or Perl:

# awk 'BEGIN{printf "%3.2f\n", 1/8+1}'
1.12

# awk 'BEGIN{printf "%3.2f\n", 2/3}'
0.67

# perl -e 'BEGIN{printf "%3.2f\n", 2/3}'
0.67

Regards!

...JRF...
Vadim Loginov
Advisor

Re: Mathmatical term in the script

Hi,

I prefer to use 'bc'. And it is easy to use with negative numbers:


print "scale=0; 24 + 3.23" | bc
print "scale=0; -24 + 3" | bc
print "scale=2; -24 / 7" | bc

var1=-24
var2=7
print "scale=5; $var1 / $var2" | bc

output:

27.23
-21
-3.42
-3.42857

Regards,
Vadim

Vadim Loginov
Advisor

Re: Mathmatical term in the script

Hi,

I prefer to use 'bc'. And it is easy to use with negative numbers:


print "scale=2; 24 + 3.23" | bc
print "scale=0; -24 + 3" | bc
print "scale=2; -24 / 7" | bc

var1=-24
var2=7
print "scale=5; $var1 / $var2" | bc

output:

27.23
-21
-3.42
-3.42857

Regards,
Vadim

Vadim Loginov
Advisor

Re: Mathmatical term in the script

Hi,

I prefer to use 'bc'. And it is easy to use with negative numbers:


print "scale=2; 24 + 3.23" | bc
print "scale=2; -24 + 3" | bc
print "scale=2; -24 / 7" | bc

var1=-24
var2=7
print "scale=5; $var1 / $var2" | bc

output:

27.23
-21
-3.42
-3.42857

Regards,
Vadim