Comparing two numbers with decimal point


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two numbers with decimal point
# 22  
Old 11-09-2009
Quote:
Originally Posted by cfajohnson

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


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

Not much different from the pipe, as a subshell adds very little time compared to that of a new process.
That's your problem isn't it? If I can run it in 0.003s , that says something about our hardware differences. also, its not even 1s. to a user , its NOT A BIG DEAL

Quote:
What maintenance? It's a black box function that was written once, many years ago
and hasn't been touched since.
what if it breaks in the future or one need to modify something to suit one's need.. he will have to read the code eventually.

Quote:
No, I now write one line of code: fpmul ...
but you need to write many lines before you could do that. a > b or a < b syntax is almost universal in every language and understandable.

Quote:
Absolute nonsense.
The shell is a very good programming language. It is the only one I need.
doesn't mean its the only one I (or others) need.


Quote:
File globbing and external commands are seamless in the shell; not so in other languages.
there are far more many other things that they do well over the shell. We don't do just file globbing all our lives. What we do more often than globbing are parsing things. Big files , small files, formatting reports. Under such cases, and as you mentioned shell is no good for big files, a better tool is more advisable.

Quote:
How often have you seen something like:

Code:
int=$(echo $x | awk -F. '{print $1}')
dec=$(echo $x | awk -F. '{print $2}')

of course, i wouldn't use that kind of programming either. I would just do them in awk ( or a real programming language)

Quote:
Speed of execution is still very important.
not so much using today's hardware processor.

Quote:
The difference between a command that executes immediately and one that takes a second or two is the difference between a good user experience and a bad one.
a second or two doesn't make a difference. Nobody cares, (maybe except you). If it's more than 30 minutes maybe it will make a difference.
Quote:
Development of shell programs can be just as fast as writing perl or python, and just as legible.
how so ? example?

Are you an academic? Or are a *nix administrator?

Last edited by ghostdog74; 11-09-2009 at 12:14 AM..
# 23  
Old 11-09-2009
Quote:
Originally Posted by cfajohnson
Like bash, expr only does integer arithmetic; you cannot use it with decimal fractions.
I had that doubt after posting the example. Thank you for pointing it out.

Quote:
In any POSIX shell, you can strip the fraction and compare the integer portions. If they are equal, then compare the decimals:
Code:
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

I like this approach.
Thanks again!
# 24  
Old 11-09-2009
Quote:
Originally Posted by ghostdog74
@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.
ksh93 can do it too and it is very fast.
# 25  
Old 11-09-2009
Quote:
Originally Posted by Scrutinizer
ksh93 can do it too and it is very fast.
good to know. and so are others that have this capability.
# 26  
Old 11-09-2009
Quote:
Originally Posted by Scrutinizer
OK, and what about:
Code:
echo "12.13>12.14"|/usr/bin/xpg6/bc; echo "12.14>12.13"|/usr/bin/xpg6/bc


no use see below:-

Code:
bash-3.00$ echo "12.13>12.14"|/usr/xpg6/bin/bc; echo "12.14>12.13"|/usr/xpg6/bin/bc

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

SmilieSmilieSmilie

---------- Post updated at 04:21 AM ---------- Previous update was at 04:18 AM ----------

Quote:
Originally Posted by radoulov
On Solaris you can also use ksh93 (you may have it under /usr/dt/bin/dtksh), zsh or expr.

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

or:

Code:
$ bash -c '(( $(expr 1.20 \> 1.3) == 1 )) && echo ok || echo ko' 
ko

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


Thanks man your solution worked fine..SmilieSmilieSmilieSmilie
# 27  
Old 11-09-2009
Quote:
Originally Posted by ahmad.diab
no use see below:-

Code:
bash-3.00$ echo "12.13>12.14"|/usr/xpg6/bin/bc; echo "12.14>12.13"|/usr/xpg6/bin/bc

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

SmilieSmilieSmilie

Well happy hunting for a bc that actually works on Solaris then.
In the mean time, use ksh93, which as you stated works well.

Last edited by Scrutinizer; 11-09-2009 at 06:02 AM..
# 28  
Old 11-09-2009
Quote:
Originally Posted by ahmad.diab

---------- Post updated at 04:21 AM ---------- Previous update was at 04:18 AM ----------
[...]
Thanks man your solution worked fine..SmilieSmilieSmilieSmilie
Note that as cfajohnson already pointed out, expr supports only an integer arithmetic (hence my expr examples are wrong).

With bc on Solaris and the relational expressions I get syntax error too.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

3. Shell Programming and Scripting

Moving decimal point in a series of numbers

Hi, i need to move the decimal point from a file listing some numbers like this : 49899.50 49914.55 49894.48 49939.65 49879.44 49919.57 49934.62 49944.67 49954.72 (1 Reply)
Discussion started by: Board27
1 Replies

4. Shell Programming and Scripting

ksh Arithmetic syntax error while comparing decimal numbers

Hello, I am having a problem when i execute following script on RHEL 6.4. Same script works fine on another machine where I have same version of RHEL and KSH. Below is the rpm and RHEL version. ossvm12(0)> rpm -qa | grep ksh ksh-20100621-19.el6.x86_64 ossvm12(0)> cat... (7 Replies)
Discussion started by: Adithya Gokhale
7 Replies

5. Shell Programming and Scripting

awk decimal point numbers matching

Hi All, Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below. $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?$/) print}' $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?/) print}' 01#.01 $ Regards, Rmkganesh. (3 Replies)
Discussion started by: rmkganesh
3 Replies

6. Shell Programming and Scripting

Comparing decimal numbers between 0 and 1

For numbers between 0 and 1 the below logic is not working. Output of above shall be "correct" but its echoing "incorrect".Kindly suggest a=.1 if then echo correct else echo incorrect fi Video tutorial on how to use code tags in The UNIX and Linux Forums. (3 Replies)
Discussion started by: itsvikas
3 Replies

7. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

8. Shell Programming and Scripting

Comparing Decimal Numbers

Im trying to compare two numbers with decimals but its not working as expected. a=1 b=1.1 if then echo "equal" fi When I do this it says that the numbers are equal. Ultimately Im using -le and -ge in the if statements but I tested with -eq for simplicity. Any way to make this... (3 Replies)
Discussion started by: Grizzly
3 Replies

9. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

10. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies
Login or Register to Ask a Question