GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Operating System  >  Shell Scripting
Go To First  |  Previous Question  |  Next Question 
 Shell Scripting  |  Question 32 of 47    Print  
How to compare two floating point numbers ?
how to perform the Arithmetic operations on floating point numbers ?


  
Total Answers and Comments: 5 Last Update: August 19, 2009     Asked by: Jitu 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
March 26, 2007 07:54:10   #1  
Amaresh Das        

RE: How to compare two floating point numbers ?
a 3.10
b -3.10

((c $a+$b))
if [[ $c -eq 0.00 ]]; then
echo "$a is equal to $b"
else
echo "$a is not equal to $b"
fi;


--------------------------------------------
a 3.10
b -4.00

if [[ $a < $b ]]; then
echo "$a is less than $b"
else
echo "$a is greater than $b"
fi;

 
Is this answer useful? Yes | No
March 28, 2007 07:38:56   #2  
Rahul        

RE: How to compare two floating point numbers ?
You can take this example

x 2.2
$ if [ $x 2.2 ]
> then
> echo "The numbers compared"
> else
> echo "Not compared"
> fi
The numbers compared

 
Is this answer useful? Yes | No
April 13, 2007 07:32:58   #3  
Jitu        

RE: How to compare two floating point numbers ?
Hi Amaresh & Rahul

Could you please recheck your answers..
I think we can't perform addition directly with variables in Shell Scripting.

As Amresh you told.
c $a+$b rather we may require c `expr $a + $b`..
Please make me clear if i am wrong..

Hi Rahul as you have written if [ $a -eq 10.5 ]....this condition will always be true while executing...

Actually what i came to know that we can't perform Arithmetic Operation with Floating Point Numbers using "expr"...

Please help me to understand it clearly...

Thanking all of you..
:-)

 
Is this answer useful? Yes | No
April 17, 2007 04:28:52   #4  
Gaurav        

RE: How to compare two floating point numbers ?
You can make you og 'bc' command to do the arithmatic operations in shell script.
Following script illustrates a sample implimentation

a 1.2
b 3.1
x $(echo $a+$b|bc)
echo [$x]

Where the arithmatic operation that is desired is marked in bold.

 
Is this answer useful? Yes | No
August 18, 2009 10:38:00   #5  
abhimishraoist Member Since: August 2009   Contribution: 1    

RE: How to compare two floating point numbers ?
We can use test also in command line

test 1.2 -gt 1.1 | echo abhishek

You will get o/p

abhishek.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape