Comparing two numbers with decimal point

How to compare two numbers with decimal points ?

Is there a way in bash to do this?

I would say bc (type man bc)

Or use ksh93 instead of bash

you have awk at your disposal.

$ awk 'BEGIN{if(12.14>12.13){ print "greater"}}'
greater

ksh93:

if (( 12.14>12.13 )); then echo greater; fi

bash:

if (( $(bc <<< "12.14>12.13") > 0 )); then echo greater; fi

posix:

if [ $(echo "12.14>12.13"|bc) -gt 0 ] ; then echo greater; fi
1 Like
bash and posix expression didn't work for me!!!!..I have the below OS.

SunOS server2 5.10 Generic_118833-36 sun4u sparc SUNW,Netra-210

Bash I am not surprised since I gather that is not a happy marriage with Solaris (is it even installed on your system?). However the problem with posix compliant version puzzles me. What is the output of this command on your system:

echo "12.13>12.14"|bc; echo "12.14>12.13"|bc
Yes I have bash version 3.0 on my system and the error massage when putting 

bash code
syntax error on line 1, teletype
bash: ((: > 0 : syntax error: operand expected (error token is "> 0 ")

posix code
syntax error on line 1, teletype
bash: [: -gt: unary operator expected

OK, so what is the output of

echo "12.13>12.14"|bc; echo "12.14>12.13"|bc
output:-

syntax error on line 1, teletype
syntax error on line 1, teletype

OK, and:

echo "12.13 > 12.14"|bc; echo "12.14 > 12.13"|bc

just it give me the same error in below and that's it

syntax error on line 1, teletype
syntax error on line 1, teletype

OK, and what about:

echo "12.13>12.14"|/usr/bin/xpg6/bc; echo "12.14>12.13"|/usr/bin/xpg6/bc

On Solaris you can also use ksh93 (you may have it under /usr/dt/bin/dtksh), zsh or expr.

% zsh -c '((1.20 > 1.3)) && print ok || print ko'
ko

or:

$ bash -c '(( $(expr 1.20 \> 1.3) == 1 )) && echo ok || echo ko' 
ko
bash -c 'case $(expr 1.20 \> 1.3) in (1) echo ok;; (*) echo ko;; esac'
ko

Like bash, expr only does integer arithmetic; you cannot use it with decimal fractions.

In any POSIX shell, you can strip the fraction and compare the integer portions. If they are equal, then compare the decimals:

if [ ${1%.*} -gt  ${2%.*} ]
then
  printf "%s is larger than %s\n" "$1" "$2"
elif [ ${1%.*} -eq  ${2%.*} ]
then
  : decimal comparison left as exercise for the reader 
else
  printf "%s is smaller than %s\n" "$1" "$2"
fi
1 Like

@OP, use awk or bc to do the job of math calculation. there's no reason you should stick to bash with its limitation on working with floats.

There's no need to use an external program when it can be done many times faster with bash (or any POSIX shell).

the idea of not using external program to do stuff that the shell can't do any better is a bit illogical. Can you show an example where using the shell to do what the OP wants to do as compared to an external program such as awk (or others) puts using the shell at a speed advantage? If possible, show how much faster it can get.

On the other hand, what the OP is doing might be part of a bigger project and not just comparing 2 numbers. In that kind of scenario, its not even wise to use just the shell. In fact, a better programming language should be used. One other thing you neglected in terms of "how much faster" is the development time of creating the script and coping with "compatibility issues". Imagine how much time one will waste to come up with the solution that you proposed, especially comparing the decimal part. It takes so much less time when a simple syntax of a > b is enough to do the job (and simpler to understand too)

Just $0.02

When dealing with a string, an external program is many, many times slower than using shell internals.

$ x=123.456
$ time echo "${x%.*}"
123

real    0m0.000s
user    0m0.000s
sys     0m0.000s
$ time echo "$x" | awk -F. '{print $1}'
123

real    0m0.045s
user    0m0.002s
sys     0m0.001s
$ time echo "$x" | sed 's/\(.*\)\..*/\1/'
123

real    0m0.048s
user    0m0.000s
sys     0m0.004s

In the time it take to create the new process, a hundred (give or take) lines of shell code can be executed.

I have an 80-line shell function, fpmul, which multiplies floating point numbers.

$ time fpmul 12.34 56.78 90.12
63143.947824

real    0m0.000s
user    0m0.000s
sys     0m0.001s

Awk is much slower:

$ time awk 'BEGIN { print 12.34 * 56.78 * 90.12; exit }'
63143.9

real    0m0.033s
user    0m0.000s
sys     0m0.001s

That is exactly when a more efficient use of the shell is important.

On the contrary, complicated programs can be written in the shell that perform faster than those using another language.

There are times when an external program is better, especially dealing with large files.

Calling an external command to deal with a single string is the most inefficient code possible.

Write for the POSIX shell and there are no compatibility issues.

Write for bash or ksh when extra efficiency is needed for specific tasks.

Since most people will have some familiarity with the shell, it is faster to learn to use it well than to learn a new programming language.

not many times slower.... you are using echo to pipe to awk, an extra pipe process increase the time.

# time awk -v v="$x" -F"." 'BEGIN{print $1}'

real    0m0.003s
user    0m0.001s
sys     0m0.001s

# time echo "$x" | awk -F. '{print $1}'
123

real    0m0.004s
user    0m0.001s
sys     0m0.003s


its just 0.003s. not that big a difference. Besides that, for an apple to apple comparison, its not even accurate. The above awk statement already had the 2nd field stored in memory and can be used anytime, whereas the shell one does not.

who would want to write and maintain an 80 lines code instead of one line ? your timing and my timing may differ since we may have different processors. Here's my timing:

$ time awk 'BEGIN { print 12.34 * 56.78 * 90.12; exit }'
63143.9

real    0m0.003s
user    0m0.000s
sys     0m0.002s

same with bc

$ time echo "12.34 * 56.78 * 90.12" |bc
63143.47

real    0m0.003s
user    0m0.000s
sys     0m0.005s

In the end, it doesn't really matter a big deal. The only big deal is, you write 80 lines of code to perform only a mere 0.003s difference with a one liner.

that's not true . Complicated programs should not be written with shell, period. They should be written with languages like Python, or Perl with modules. There are simply many things that shell can't do very well.

a tool that can process small and large files efficiently is still better than one that only works well on small files

not really true. the shell's syntax is not exactly friendly to use. A programming language for example Python has easy to read syntax, and with that respect, reading and deciphering code is "faster", thereby easier to learn than shell.

Nowadays its not about speed of program execution, but speed of script development and easier code maintenance.