floating point - Float comparison issues in Perl -


possible duplicate:
how fix perl code 1.1 + 2.2 == 3.3?

i'm working on perl script compares strings representing gene models , prints out summary of comparison. if gene models match perfectly, print out terse summary, if different, summary quite verbose.

the script looks @ value of variable determine whether should terse or verbose summary--if variable equal 1, should print terse summary; otherwise, should print verbose summary.

since value numeric (a float), i've been using == operator comparison.

if($stats->{overall_simple_matching_coefficient} == 1) {   print "gene structures match perfectly!\n"; } 

this worked correctly of tests , of new cases running now, found weird case value equal 1 above comparison failed. have not been able figure out why comparison failed, , stranger yet, when changed == operator eq operator, seemed work fine.

i thought == numerical comparison , eq string comparison. missing here?

update: if print out value right before comparison...

printf("test: '%f', '%d', '%s'\n", $stats->{overall_simple_matching_coefficient}, $stats->{overall_simple_matching_coefficient}, $stats->{overall_simple_matching_coefficient}); 

...i this.

test: '1.000000', '0', '1' 

the first thing computer language teacher should teach computer language you cannot compare floats equality. true of language. floating point arithmetic not exact, , 2 floats they're same different in insignificant digits somewhere can't see it. instead, can compare close each other -

if (abs(stats->{overall_simple_matching_coefficient)-1) < 0.0001) 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -