The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating point error in C Hara High Level Programming 2 06-18-2008 06:43 AM
how to compare big real numbers padarthy Shell Programming and Scripting 4 10-02-2007 06:23 AM
floating point problem vijlak High Level Programming 4 03-08-2007 04:18 AM
floating point addition ravi raj kumar Shell Programming and Scripting 8 12-22-2006 02:47 AM
problem with floating point numbers in awk kanagias Shell Programming and Scripting 7 06-24-2005 04:14 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2007
padarthy padarthy is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 15
How to Compare Floating point / real numbers

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
  #2 (permalink)  
Old 09-18-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,926
With zsh:


Code:
% [[ 0.3 -gt 0.1 ]]&& echo OK||echo KO
OK
% [[ 0.3 -lt 0.1 ]]&& echo OK||echo KO
KO

  #3 (permalink)  
Old 09-18-2007
SanjayLinux SanjayLinux is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 58
Wink Try This one

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


~~~Sanjay~~~
  #4 (permalink)  
Old 09-18-2007
virmin virmin is offline
Registered User
  
 

Join Date: Jun 2005
Location: Beijing
Posts: 13
You can do it by using bc

You also can achieve that by using bc

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

if [ $Result -eq 1 ]
then
echo "value1 is greater"
fi
  #5 (permalink)  
Old 09-18-2007
padarthy padarthy is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 15
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
  #6 (permalink)  
Old 09-18-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
You are reading value1 and echoing Value1. (And ditto for value2.) value1 and Value1 are 2 different variables.
  #7 (permalink)  
Old 09-18-2007
virmin virmin is offline
Registered User
  
 

Join Date: Jun 2005
Location: Beijing
Posts: 13
Talking

Quote:
Originally Posted by padarthy View Post
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0