-2

Compare the round half up values of num1=172 and num2=172.8, where num2's value will be 173.

Print whether or not num1 and num2 are equal.

if (( $(echo "$num1 == $num2" | bc -l) )); then
 echo "num1 and num2 are equal"
else
 echo "number are not close to each other'
fi
agc
  • 7,973
  • 2
  • 29
  • 50
Raghu
  • 51
  • 2
  • You want to round a number to a whole number and compare them then? What is "roundoff" and "roundoff 5" (why 5?) ? – KamilCuk Jul 31 '21 at 12:04
  • I mean if 172.1= round of value is 172, incase if decimal 5 & >5 like 172.5,172.6,172.7, etc should be 173.. round of 5 indicate value 5 and >5 – Raghu Jul 31 '21 at 13:43
  • `ncase if decimal 5` https://en.wikipedia.org/wiki/Rounding#Rounding_to_the_nearest_integer – KamilCuk Jul 31 '21 at 13:48

2 Answers2

2

Use https://en.wikipedia.org/wiki/Dynamic_programming . Your problem consist of:

  • rounding numbers to zero decimal digits
  • comparing the results

The first part can be found on stackoverflow, like Round a divided number in Bash , the second part can be done with just == even with string comparison.

round() {
    printf "%.${2:-0}f" "$1"
}
num1=172
num2=172.8 
if (( $(round "$num1") == $(round "$num2") )); then
    echo "Equal"
else
    echo "Not equal"
fi

The (( arithmetic expression is specific to Bash shell.


You can compare them in bc using the same method. First take a rounding function from https://github.com/zg/bc/blob/master/code/funcs.bc and then compare the rounded numbers:

if (($(bc -l <<EOF
define int(x)   { auto os;os=scale;scale=0;x/=1;scale=os;return(x) }
int($num1) == int($num2)
EOF
   ) )); then
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Hmm... the `((...))` arithmetic operator is POSIX. I think you are referring to the [Arithmetic Commands](https://mywiki.wooledge.org/ArithmeticExpression) section where bash allows assignment within `((...))`. – David C. Rankin Aug 01 '21 at 00:14
  • Arithmetic expansion `$((...))` is POSIX. Arithmetic expansion is replaced by the result of the expression, but exit status is always 0. Arithmetic expression `((...))` is not in POSIX. Arithmetic expression exits with zero/non-zero exit status when the result of expression is non-zero/zero. In POSIX `if [ "$((...))" -ne 0 ]; then`, in bash just `if (( .. )); then`. :D – KamilCuk Aug 01 '21 at 07:20
  • Ooh ... I like it. I'd not thought about the distinguishing quality being the return but that makes perfect sense. – David C. Rankin Aug 02 '21 at 03:45
0

No bash, no external utils, just pure (ugly) POSIX shell code in two functions:

rhup () 
{ 
    [ "${1##*.[5-9]*}" ]
    echo "$((${1%%.*}+$?))"
}

req () 
{ 
    a=
    [ "$(rhup "$1")" = "$(rhup "$2")" ] || a="not "
    echo "When rounded half up $1 and $2 are ${a}equal."
}

Demo:

req 2 3 ; req 2 2.2 ; req 2.4 2.5 

Output:

When rounded half up 2 and 3 are not equal.
When rounded half up 2 and 2.2 are equal.
When rounded half up 2.4 and 2.5 are not equal.

How it works:

Given a number rhup (short for round half up) uses shell parameter substitution to check if a half-up decimal suffix exists. Then it adds the resulting error code of 0 or 1 to the number's integer prefix and prints the sum.

req (short for rhup equal) runs rhup on two numbers, compares them, if they're not equal, sets $a to "not ", then prints the desired conditional English sentence.

Neither function does any error checking on the input values.

agc
  • 7,973
  • 2
  • 29
  • 50