# How to Compare Floating point / real numbers

**URL:** <https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668>\
**Category:** Shell Programming and Scripting\
**Created:** [September 18, 2007, 4:18am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668 "2007-09-18T04:18:44Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![padarthy](https://community.unix.com/letter_avatar/padarthy/32/5_5575768a8748004e209b776fc1b2916d.png) [@padarthy](https://community.unix.com/u/padarthy)\
**Post date:** [September 18, 2007, 4:18am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/1 "2007-09-18T04:18:44Z")

</div>

Hai,

Can you please guide me, to compare the floating point numbers.

Eg.

If [$value1 \> $value2]  
then

echo "value1 is grater "

fi

This code is not working properly when i excuted with floating values or real numbers

---

<div class="post-metadata">

**Author:** ![radoulov](https://community.unix.com/user_avatar/community.unix.com/radoulov/32/489_2.png) [@radoulov](https://community.unix.com/u/radoulov)\
**Post date:** [September 18, 2007, 5:10am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/2 "2007-09-18T05:10:13Z")

</div>

With zsh:

```nohighlight
% [[0.3 -gt 0.1]]&& echo OK||echo KO
OK
% [[0.3 -lt 0.1]]&& echo OK||echo KO
KO

```

---

<div class="post-metadata">

**Author:** ![SanjayLinux](https://community.unix.com/letter_avatar/sanjaylinux/32/5_5575768a8748004e209b776fc1b2916d.png) [@SanjayLinux](https://community.unix.com/u/SanjayLinux)\
**Post date:** [September 18, 2007, 5:14am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/3 "2007-09-18T05:14:18Z")

</div>

HI,

```
I hope you can do it by awk. For me it is working fine

```

----------------------------------------------------------------------  
# x=3.1; y=3.2; echo "$x $y" | awk '{if ($1 \> $2) print $1; else print $2}'  
3.2

-----------------------------------------------------------------------  
I'm sure you can adapt it to your script

```auto

```

---

<div class="post-metadata">

**Author:** ![virmin](https://community.unix.com/letter_avatar/virmin/32/5_5575768a8748004e209b776fc1b2916d.png) [@virmin](https://community.unix.com/u/virmin)\
**Post date:** [September 18, 2007, 5:40am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/4 "2007-09-18T05:40:16Z")

</div>

You also can achieve that by using bc

Result=`echo "$Value1 \> $Value2" | bc`

if [$Result -eq 1]  
then  
echo "value1 is greater"  
fi

---

<div class="post-metadata">

**Author:** ![padarthy](https://community.unix.com/letter_avatar/padarthy/32/5_5575768a8748004e209b776fc1b2916d.png) [@padarthy](https://community.unix.com/u/padarthy)\
**Post date:** [September 18, 2007, 10:21pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/5 "2007-09-18T22:21:53Z")

</div>

Hi Virmin,

```
After excuting your code. i am getting a syntax error .

echo "Enter value1"
read value1
echo "Enter value2"
read value2

Result=\`echo "$Value1 &gt; $Value2" | bc \`

```

if [$Result -eq 1]  
then  
echo "$value1 is greater"  
fi

```
 Error:
 \-----
 syntax error on line 1, teletype
```

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [September 18, 2007, 10:26pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/6 "2007-09-18T22:26:13Z")

</div>

You are reading value1 and echoing Value1. (And ditto for value2.) value1 and Value1 are 2 different variables.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [September 18, 2007, 10:41pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/7 "2007-09-18T22:41:47Z")

</div>

```nohighlight
#!/bin/ksh

a=1.72
b=1.71

if ["$(echo "if (${a} > ${b}) 1" | bc)" -eq 1] ; then
   echo ">"
else
   echo "<"
fi;

```

---

<div class="post-metadata">

**Author:** ![virmin](https://community.unix.com/letter_avatar/virmin/32/5_5575768a8748004e209b776fc1b2916d.png) [@virmin](https://community.unix.com/u/virmin)\
**Post date:** [September 18, 2007, 10:56pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/8 "2007-09-18T22:56:14Z")

</div>

> [@padarthy](#):
>
> Hi Virmin,
> 
> After excuting your code. i am getting a syntax error .
> 
> echo "Enter value1"  
> read value1  
> echo "Enter value2"  
> read value2
> 
> Result=`echo "$Value1 \> $Value2" | bc `
> 
> if [$Result -eq 1]  
> then  
> echo "$value1 is greater"  
> fi
> 
> Error:  
> -----  
> syntax error on line 1, teletype

as Perderabo said, unix is case sensitive, so value1 and Value1 are totally different variable.  
and you have to make sure $Value1 and $Value2 are legal numbers before you use them, or bc will fail.

---

<div class="post-metadata">

**Author:** ![padarthy](https://community.unix.com/letter_avatar/padarthy/32/5_5575768a8748004e209b776fc1b2916d.png) [@padarthy](https://community.unix.com/u/padarthy)\
**Post date:** [September 19, 2007, 1:44am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/9 "2007-09-19T01:44:37Z")

</div>

I corrected the case sensitive, Still i am getting an error

echo "Enter value1"  
read value1  
echo "Enter value2"  
read value2

Result=`echo $value1 \> $value2 | bc `

if [$Result -eq 1]  
then  
echo "$value1 is greater"  
fi

ouput  
-----

bash-2.03$ ./compare  
Enter value1  
5.6  
Enter value2  
3.2  
./compare[9]: test: argument expected

Please correct this.

---

<div class="post-metadata">

**Author:** ![SanjayLinux](https://community.unix.com/letter_avatar/sanjaylinux/32/5_5575768a8748004e209b776fc1b2916d.png) [@SanjayLinux](https://community.unix.com/u/SanjayLinux)\
**Post date:** [September 19, 2007, 3:45am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/10 "2007-09-19T03:45:47Z")

</div>

Result=`echo $value1 \> $value2 | bc `

Here Im seeing one whitespace in the end of above command.

so please remove that whitespace.

Result=`echo $value1 \> $value2 | bc`

Thanks  
~~Sanjay Tripathi~~

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [September 19, 2007, 4:03am UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/11 "2007-09-19T04:03:09Z")

</div>

> [@padarthy](#):
>
> echo "Enter value1"  
> read value1  
> echo "Enter value2"  
> read value2
> 
> Result=`echo $value1 \> $value2 | bc `
> 
> if [$Result -eq 1]  
> then  
> echo "$value1 is greater"  
> fi

Change

```plaintext
if [$Result -eq 1]

```

to

```plaintext
if [[$Result -eq 1]]

```

This will only resolve the error that you are seeing.

---

<div class="post-metadata">

**Author:** ![padarthy](https://community.unix.com/letter_avatar/padarthy/32/5_5575768a8748004e209b776fc1b2916d.png) [@padarthy](https://community.unix.com/u/padarthy)\
**Post date:** [September 19, 2007, 9:53pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/12 "2007-09-19T21:53:45Z")

</div>

Vino, after changing the code..

I am not getting the output .. No errors were display

---

<div class="post-metadata">

**Author:** ![blowtorch](https://community.unix.com/user_avatar/community.unix.com/blowtorch/32/136_2.png) [@blowtorch](https://community.unix.com/u/blowtorch)\
**Post date:** [September 19, 2007, 11:07pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/13 "2007-09-19T23:07:19Z")

</div>

Er, the input to bc is incorrect. It will give you an error. Use this:

```nohighlight
echo "Enter value1"
read value1
echo "Enter value2"
read value2

Result=`echo "if($value1 > $value2) 1" | bc `

if [$Result -eq 1]; then 
   echo "$value1 is greater"
fi

```

vgersh99 has very similar code in his post... you could have used that as well.

---

<div class="post-metadata">

**Author:** ![padarthy](https://community.unix.com/letter_avatar/padarthy/32/5_5575768a8748004e209b776fc1b2916d.png) [@padarthy](https://community.unix.com/u/padarthy)\
**Post date:** [September 24, 2007, 8:03pm UTC](https://community.unix.com/t/how-to-compare-floating-point-real-numbers/175668/14 "2007-09-24T20:03:21Z")

</div>

Hi I am getting the error

Here is the output

bash-2.03$ ./file4  
Enter value1  
4568456  
Enter value2  
6589546  
./file4: [: -eq: unary operator expected

Please correct this code

echo "Enter value1"  
read value1  
echo "Enter value2"  
read value2

Result=`echo "if($value1 \> $value2) 1" | bc `

if [$Result -eq 1]; then  
echo "$value1 is greater"  
fi
