- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How can I do floating point arithmetic in ksh ?
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
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
тАО01-30-2001 02:12 AM
тАО01-30-2001 02:12 AM
Every time I try to do floating point aithmetic it detaults to integer values. e.g.
> x=1.1; echo $x
1.1
> let "x=x+1.5"; echo $x
2
> let "x=x+1.9" ; echo $x
3
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2001 02:27 AM
тАО01-30-2001 02:27 AM
Re: How can I do floating point arithmetic in ksh ?
As shells default to integer arithmetic, the only way I know to enable real arithmetic is through 'bc'
You could use the 'two way pipe' to the program to do all your math operations, like this:
#!/usr/bin/sh
# This starts bc with a two-way pipe
bc |&
# print -p writes to the pipe
print -p scale=2
print -p 3/4
# read -p reads from the pipe
read -p myvar
echo $myvar
The script here above will echo '.75'
see manual for sh-posix
Best regards,
Dan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2001 02:27 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-30-2001 05:01 AM
тАО01-30-2001 05:01 AM
Re: How can I do floating point arithmetic in ksh ?
Certainly, 'bc' is the first tool that springs to mind for dealing with real numbers in shells. 'awk' can also be used:
Thus, either:
# X=1;Y=8;echo "scale=3\n $X / $Y" | bc
...or....
# X=1;Y=8;echo "$X $Y" | awk '{printf "%.4f\n",$1 / $2}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-31-2001 08:45 AM
тАО01-31-2001 08:45 AM
Re: How can I do floating point arithmetic in ksh ?
#!/usr/dt/bin/dtksh
float x=5 y=3 z
print $(( z = $x/$y)) #or
printf "%.4f\n" $(( z = $x/$y ))