- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Floting point operation
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2011 01:44 PM
тАО02-10-2011 01:44 PM
Here my script
for instance in $(.........)
do
(( mySum= 0.0000 ))
#
for PID in $(.....)
do
lecture=$( here a call to a function returning float value )
echo $instance $lecture
(( mySum = $mySum + $lecture ))
done
print "mySum=" $mySum
done
and the result:
(Example)
opclmqa1 15.0
opclmqa1 0.1
opclmqa1 0.2
opclmqa1 0.0
mySum= 15
I should have 15.3
why ? and how to fix ?
Bests Regards
Den
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2011 01:53 PM
тАО02-10-2011 01:53 PM
Re: Floting point operation
Most shells only support integer arithmetic.
# X=100;Y=25;((Z=X/Y));echo $Z
If you want to incorporate real numbers, 'bc' provides a convenient vehicle. Choose the 'scale' to set the precision you want:
# X=1;Y=8;echo "scale=3\n $X/$Y"|bc
You can also leverage 'awk' and couple it with 'printf' to format output, including moving to a different number base:
# X=1;Y=8;echo "$X $Y"|awk '{printf "%.4f\n",$1/$2}'
# X=7;Y=8;echo "$X $Y"|awk '{printf "%4x\n",$1+$2}'
The standard Korn shell in HP-UX is Korn88 and does integer arithmetic. However, if you use the Korn93 (found in HP-UX as '/usr/dt/bin/dtksh') shell, its 'typeset' allows a more direct approach to real numbers:
#!/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
exit 0
Regards!
...JRF...
- Tags:
- dtksh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2011 02:03 PM
тАО02-10-2011 02:03 PM
Re: Floting point operation
HUmm, but how to do a sum in this case ? it seems difficult like this.
Have a nice end day
Thanks James.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-10-2011 02:21 PM
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2011 05:56 AM
тАО02-11-2011 05:56 AM
Re: Floting point operation
Finally I prefer to build my list
and then I'm able to have this ...
echo $a
1.1 1.0 23.10 1.4 0.8 0.0 0.0
How to have the sum of all float number in my variable ?
Bests regards
Denis A. Jeanneret
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2011 06:13 AM
тАО02-11-2011 06:13 AM
Re: Floting point operation
With a pipe against awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}'
Thanks James, have a nice week end
Bests regards
Den
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-11-2011 06:13 AM
тАО02-11-2011 06:13 AM
Re: Floting point operation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-12-2011 02:34 AM
тАО02-12-2011 02:34 AM
Re: Floting point operation
Instead of looping in awk, you can loop in the shell:
for i in $a; do echo $i; done |
awk '{SUM+=$1};END{printf "%6.1f\n",SUM}'