Operating System - Linux
1752777 Members
6629 Online
108789 Solutions
New Discussion юеВ

Re: Floating point math in ksh93

 
SOLVED
Go to solution
dictum9
Super Advisor

Floating point math in ksh93


I need to be able to convert MB into GB, e.q. if I have 19720 MB, I need to know that it's 19.3 GB, not just 19 as ksh reports.

6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Floating point math in ksh93

Hi:

# cat .float
#!/usr/dt/bin/dtksh
typeset -F R1
typeset -F3 R2
typeset -E R3
let R1=1/8
echo ${R1}
let R2=1/8
echo ${R2}
let R3=1/8
echo ${R3}
# ./float
.1250000000
.125
0.125
#

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Floating point math in ksh93

You can of course use integer scaled math:
19720 * 100 / 1024

Then you would have to add the "." before the 2 decimal places.

Or you could use awk to do the calculation.
Oviwan
Honored Contributor

Re: Floating point math in ksh93

Hey

what would you do with decimals?

you can't compare them:
$ [[ 1.2 -eq 1.5 ]] && echo ok
ok
$ [[ 1.2 -gt 1.5 ]] && echo ok
$ [[ 2.2 -gt 1.5 ]] && echo ok
ok
$ [[ 2.2 -gt 2.5 ]] && echo ok

Regards
Dennis Handly
Acclaimed Contributor

Re: Floating point math in ksh93

>Oviwan: what would you do with decimals?

Did you read the subject?
Those commands work perfectly fine with /usr/dt/bin/dtksh
Oviwan
Honored Contributor

Re: Floating point math in ksh93

ups you are right, sorry for confusion!

$ /usr/dt/bin/dtksh
$ echo $0
/usr/dt/bin/dtksh
$ [[ 1.2 -gt 1.1 ]] && echo ok
ok
$ [[ 1.1 -gt 1.1 ]] && echo ok
$ [[ 2.1 -gt 1.1 ]] && echo ok
ok
$ [[ 1.2 -eq 1.5 ]] && echo ok
$ [[ 1.2 -gt 1.5 ]] && echo ok
$ [[ 2.2 -gt 1.5 ]] && echo ok
ok
$ [[ 2.2 -gt 2.5 ]] && echo ok
Bill Hassell
Honored Contributor

Re: Floating point math in ksh93

dtksh is ksh93 and like all shell scripts, you should specify the desired interpreter on line 1:

#!/usr/dt/bin/dtksh

# put your ksh code here

ksh93 has a lot of enhancements over ksh88 (the /usr/bin/ksh in HP-UX) and the POSIX shell /usr/bin/sh. There are also some forward incompatibilities so be aware that a few techniques may need some changes. The dtksh shell was provided with add-on features for Xwindows but it's most useful feature today is ksh93 enhancements.


Bill Hassell, sysadmin