I can't say as I don't know what operations you intend to perform on you 'number'. In general, the practice of adding an epsilon to floats just moves the problem around and does nothing but, perhaps, increase roundoff errors. Representational errors in floats are not evenly distributed . Take for example the following code:
print "Division by N\n";
for my $n (1..100) {
if ( $n/100 == 0.01*$n ) { print "t" }
else { print "n" }
}
Division by N
ttttttttttttttttttttttttttttttttttntttttntttttntttttttttntttttttttttnn
+tttttttttttnnttttttttttnnttttt
In an ideal world the code should always return true but for some values returns false due to an imprecise binary representation for $n.
Now if you are only doing an operation once the need to check the results it is acceptable to do the equality like this: if (( x - y ) > epsilon ) then true
Also consider that 1e-8 is also not exactly representable in binary: perl -e 'our $sum;for(1..1000000){$sum+=0.00000001};printf "%1.20f\n",
+ $sum'
0.00999999999994859168
This leads to the rule: Make all constants integer multiples of 2^n. i.e.perl -e 'printf "%0.20f\n", 2/0x1000000' [11:08am]
0.00000011920928955078
This is especially important if using small divisors.
These methods falls apart if you are doing any kind of loop that allows the errors to accumulate; such as, Newtons Method, averages, Runge-Kutta, least-squares and etc. In these methods the error can grow to unanticipated magnitudes such to overwhelm a small, fixed, epsilon. Special methods are then needed that anticipate and correct for roundoff.
s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s
|-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,,
$|=1,select$,,$,,$,,1e-1;print;redo}
|