Keep It Simple, Stupid | |
PerlMonks |
Re: floating point additionby trippledubs (Deacon) |
on Jan 12, 2015 at 16:56 UTC ( [id://1112965]=note: print w/replies, xml ) | Need Help?? |
Another exercise to represent fractions in binary is to take the decimal and multiply by two in a column.
Now taking the whole number and going from bottom to top you get 101, the binary representation, exactly. If you take .1 you get
And from that point you get a repeating sequence in binary similar to .333..3 in decimal And the takeaway is instead of testing for equality in floating point you test against an Epsilon (small quantity) and if the two numbers are within that Epsilon close to each other, they are for all intents are purposes, equal. Not doing so introduces bugs.
In Section
Seekers of Perl Wisdom
|
|