P is for Practical | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Numerical methods are deceptively difficult.
They require exquisite attention to detail to make them
work properly. For example,
if you are getting dsin(30)=0.49999999999999945
then you have a serious bug.
For most hardware out there, you cannot properly initialize a variable such as pi by entering lots of digits in your program. This is because the internal precision of the machine exceeds the precision that you are normally allowed to print or initialize. So if you catch yourself typing in lots of digits of pi, you are doing something wrong. Here is a better way to do it: This prints 0.5, as you would hope that it would. As far as the snippet goes, it is seriously borked. Among other problems, it has problems with the implicit string/numeric conversion that perl uses. This causes it to give different results for
approx(".00000000001") The recent CPAN module Math::SnapTo version 0.02 also has serious errors. For good rounding, use Math::Round. It doesn't do exactly what you want (that is, round like a human), but neither does this snippet or Math::SnapTo. It should work perfectly the first time! - toma In reply to Re: Fix floats like you do in your head
by toma
|
|