Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This example works fine:

awk -v num1=5999 -v num2=5999  'BEGIN{ print (num2==num1) ? "equal" : "not equal" }'
equal

This example does not work well:

awk -v num1=59558711052462309110012 -v num2=59558711052462309110011 'BEGIN{ print (num2==num1) ? "equal" : "not equal" }'
equal

In the second example compared numbers are different. Why not print "not equal"?

share|improve this question
    
@jw013 I noticed that too, but both examples shouldn't print the same result. –  jordanm Dec 17 '12 at 15:47
    
@jw013 these are just examples. jordanm is right that the two examples should not print the same result. –  nowy1 Dec 17 '12 at 16:09
add comment

1 Answer

up vote 11 down vote accepted

You're reaching the limit of the precision of awk numbers.

You could force the comparison to be a string comparison with:

awk -v num1=59558711052462309110012 -v num2=59558711052462309110011 '
  BEGIN{ print (num2""==num1) ? "equal" : "not equal" }'

(Here the concatenation with the empty string forces them to be considered as strings instead of numbers).

If you want to do numerical comparison, you'll have to use a tool that can work with arbitrary precision numbers like bc or python.

share|improve this answer
    
Thank you for the explanation. –  nowy1 Dec 17 '12 at 16:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.