Below is the little floating-point comparison script I ran in class.
#!/usr/bin/perl use strict; use warnings; my $n = 0; for (my $i = 0; $i < 10; $i++) { $n += 0.1; } print "\$n = $n\n"; if ($n == 1.0) { print "\$n == 1.0\n"; } else { print "\$n != 1.0\n"; } if ($n < 1.0) { print "\$n < 1.0\n"; } else { print "\$n !< 1.0\n"; } if (fp_equal($n, 1.0, 10)) { print "\$n fp== 1.0\n"; } else { print "\$n fp!= 1.0\n"; } sub fp_equal { my ($left, $right, $precision) = @_; return sprintf("%.${precision}g", $left) eq sprintf("%.${precision}g", $right); }