Operating System - HP-UX
1836582 Members
1386 Online
110102 Solutions
New Discussion

How do you compare a decimal?

 
SOLVED
Go to solution
someone_4
Honored Contributor

How do you compare a decimal?

Hey everyone. Hope everyone is having a good day out there in hp land !
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
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: How do you compare a decimal?

Hi Richard:

I'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...
A. Clay Stephenson
Acclaimed Contributor

Re: How do you compare a decimal?

Hi Richard,

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
If it ain't broke, I can fix that.
someone_4
Honored Contributor

Re: How do you compare a decimal?

That worked great ..
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
James R. Ferguson
Acclaimed Contributor

Re: How do you compare a decimal?

Hi Richard:

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...
A. Clay Stephenson
Acclaimed Contributor

Re: How do you compare a decimal?

Hi Richard,

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
If it ain't broke, I can fix that.