- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How do you compare a decimal?
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
Forums
Discussions
Discussions
Discussions
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
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
07-19-2001 10:17 AM
07-19-2001 10:17 AM
My question is how do you compare a number with a decimal poing?
For example
how would you say
if 1525.258 is greater then 3218.25
here is the actuall line of the script
and the target _rates are always in decimals ..
if [[ "${target_rate_array[$x]}" -gt "${target_r
ate_array[$y]}" && "${target_rate_array[$x]}" != "" && "${target_rate_array[$y]}
" != "" ]]
then
swap="${target_rate_array[$x]}"
target_rate_array[$x]="${target_rate_array[$y]}"
target_rate_array[$y]="$swap"
swap="${target_sequence_array[$x]}"
target_sequence_array[$x]="${target_sequence_array[$y]}"
Thanks richard
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2001 10:39 AM
07-19-2001 10:39 AM
SolutionI'd use 'awk' to make the comparison. For example:
# X=8.11
# Y=8.17
# echo "$X $Y"|awk '{if ($1 > $2) print "GTR"} else {print "not GTR"}}'
...would echo the relationship
...another approach is:
# X=8.11
# Y=8.17
# echo "$X $Y"|awk '{if ($1 > $2) {exit(0)} else {exit(1)}}'
# if [ $? -eq 0 ]
> then
> echo "X gtr Y"
> else
> echo "X leq Y"
> fi
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2001 10:55 AM
07-19-2001 10:55 AM
Re: How do you compare a decimal?
You actually have two problems. First NEVER compare actual floating points in any language for equality. For example, you probably want 3.99999999 and 4.00000000001 to compare equally. The standard idiom in any language is
to do something like this if (abs(a - b) < 0.00001) then a equals b else a <> b. The other is that you want to do this in the shell.
The easy way is to convert both values to a fixed size zero padded string rounded to a desired precision and then compare the two values as strings not integers.
I'll use a width of 15 with 5 decimal places.
function fmt_float
{
echo $1 | awk '{printf "%015.5f\n" $1}'
return 0
}
A=1525.258
B=3218.25
A2=`fmt_float ${A}`
B2=`fmt_float ${B}`
if [ ${A2} < ${B2} ]
then
echo "A < B"
else
if [ ${A2} > ${B2} ]
then
echo "A > B"
else
echo "A = B"
fi
fi
Another way to do this is top invoke a bc script which can do comparions to ant desired precision.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2001 11:07 AM
07-19-2001 11:07 AM
Re: How do you compare a decimal?
Can you tell me why that works with the awk command?
I found and oreilly awk book. Im going to get that bad boy ..
Thanks allot
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2001 11:28 AM
07-19-2001 11:28 AM
Re: How do you compare a decimal?
Some call 'awk' short for "awkward". On the contrary, its an extremely powerful tool with which you can create and format reports, do arithmetic, extract fields and records from input data, etc. Like any sophisticated tool, it's only fair to judge it when you know how to use it.
Numbers in 'awk' can be integers, decimal numbers or numbers in scientific notation.
A "nice" feature of 'awk' is that strings are converted to numbers, and numbers to strings, if the context of the program demands it. [...and no, we will not argue the merits and demerits of strong variable typing, here! ;-) ].
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2001 11:30 AM
07-19-2001 11:30 AM
Re: How do you compare a decimal?
The comparison works in awk because awk is smart enough to do an implicit numeric conversion. If awk sees "23.56" is assumes that it is a numeric value rather than a character string. There are tricks you can do to force a type conversion from string to numeric or vice versa. You still need to somehow deal with extremely close values that you would like to compare as equal; that is why forcing it to round to a value is a safer method.
Clay