<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do you compare a decimal? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554781#M886643</link>
    <description>Hi Richard,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Clay</description>
    <pubDate>Thu, 19 Jul 2001 18:30:49 GMT</pubDate>
    <dc:creator>A. Clay Stephenson</dc:creator>
    <dc:date>2001-07-19T18:30:49Z</dc:date>
    <item>
      <title>How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554776#M886638</link>
      <description>Hey everyone. Hope everyone is having a good day out there in hp land ! &lt;BR /&gt;My question is how do you compare a number with a decimal poing?&lt;BR /&gt;For example &lt;BR /&gt;how would you say &lt;BR /&gt;if 1525.258 is greater then 3218.25&lt;BR /&gt;here is the actuall line of the script&lt;BR /&gt;and the target _rates are always in decimals ..&lt;BR /&gt;&lt;BR /&gt; if [[ "${target_rate_array[$x]}" -gt "${target_r&lt;BR /&gt;ate_array[$y]}" &amp;amp;&amp;amp; "${target_rate_array[$x]}" != "" &amp;amp;&amp;amp; "${target_rate_array[$y]}&lt;BR /&gt;" != "" ]]&lt;BR /&gt;                                then&lt;BR /&gt;                                        swap="${target_rate_array[$x]}"&lt;BR /&gt;                                        target_rate_array[$x]="${target_rate_array[$y]}"&lt;BR /&gt;                                        target_rate_array[$y]="$swap"&lt;BR /&gt;                                        swap="${target_sequence_array[$x]}"&lt;BR /&gt;                                        target_sequence_array[$x]="${target_sequence_array[$y]}"&lt;BR /&gt;                                  &lt;BR /&gt;&lt;BR /&gt;Thanks richard</description>
      <pubDate>Thu, 19 Jul 2001 17:17:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554776#M886638</guid>
      <dc:creator>someone_4</dc:creator>
      <dc:date>2001-07-19T17:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554777#M886639</link>
      <description>Hi Richard:&lt;BR /&gt;&lt;BR /&gt;I'd use 'awk' to make the comparison.  For example:&lt;BR /&gt;&lt;BR /&gt;# X=8.11&lt;BR /&gt;# Y=8.17&lt;BR /&gt;# echo "$X $Y"|awk '{if ($1 &amp;gt; $2) print "GTR"} else {print "not GTR"}}'&lt;BR /&gt;&lt;BR /&gt;...would echo the relationship&lt;BR /&gt;&lt;BR /&gt;...another approach is:&lt;BR /&gt;&lt;BR /&gt;# X=8.11&lt;BR /&gt;# Y=8.17&lt;BR /&gt;# echo "$X $Y"|awk '{if ($1 &amp;gt; $2) {exit(0)} else {exit(1)}}'&lt;BR /&gt;# if [ $? -eq 0 ]&lt;BR /&gt;&amp;gt; then&lt;BR /&gt;&amp;gt; echo "X gtr Y"&lt;BR /&gt;&amp;gt; else&lt;BR /&gt;&amp;gt; echo "X leq Y"&lt;BR /&gt;&amp;gt; fi&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 19 Jul 2001 17:39:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554777#M886639</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-07-19T17:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554778#M886640</link>
      <description>Hi Richard,&lt;BR /&gt;&lt;BR /&gt;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&lt;BR /&gt;to do something like this if (abs(a - b) &amp;lt; 0.00001) then a equals b else a &amp;lt;&amp;gt; b. The other is that you want to do this in the shell.&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;I'll use a width of 15 with 5 decimal places.&lt;BR /&gt;&lt;BR /&gt;function fmt_float&lt;BR /&gt;{&lt;BR /&gt;  echo $1 | awk '{printf "%015.5f\n" $1}'&lt;BR /&gt;  return 0&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;A=1525.258&lt;BR /&gt;B=3218.25&lt;BR /&gt;&lt;BR /&gt;A2=`fmt_float ${A}`&lt;BR /&gt;B2=`fmt_float ${B}`&lt;BR /&gt;&lt;BR /&gt;if [ ${A2} &amp;lt; ${B2} ]&lt;BR /&gt;  then&lt;BR /&gt;    echo "A &amp;lt; B"&lt;BR /&gt;  else&lt;BR /&gt;    if [ ${A2} &amp;gt; ${B2} ]&lt;BR /&gt;      then&lt;BR /&gt;        echo "A &amp;gt; B"&lt;BR /&gt;      else&lt;BR /&gt;        echo "A = B"&lt;BR /&gt;      fi&lt;BR /&gt;  fi&lt;BR /&gt;&lt;BR /&gt;Another way to do this is top invoke a bc script which can do comparions to ant desired precision.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Regards, Clay&lt;BR /&gt;</description>
      <pubDate>Thu, 19 Jul 2001 17:55:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554778#M886640</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-07-19T17:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554779#M886641</link>
      <description>That worked great .. &lt;BR /&gt;Can you tell me why that works with the awk command?&lt;BR /&gt;I found and oreilly awk book. Im going to get that bad boy ..&lt;BR /&gt;&lt;BR /&gt;Thanks allot&lt;BR /&gt;&lt;BR /&gt;Richard</description>
      <pubDate>Thu, 19 Jul 2001 18:07:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554779#M886641</guid>
      <dc:creator>someone_4</dc:creator>
      <dc:date>2001-07-19T18:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554780#M886642</link>
      <description>Hi Richard:&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Numbers in 'awk' can be integers, decimal numbers or numbers in scientific notation.&lt;BR /&gt;&lt;BR /&gt;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! ;-) ].&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Thu, 19 Jul 2001 18:28:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554780#M886642</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2001-07-19T18:28:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do you compare a decimal?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554781#M886643</link>
      <description>Hi Richard,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Clay</description>
      <pubDate>Thu, 19 Jul 2001 18:30:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-do-you-compare-a-decimal/m-p/2554781#M886643</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2001-07-19T18:30:49Z</dc:date>
    </item>
  </channel>
</rss>

