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:
use Math::Trig;
my $pi= 4*atan2(1,1);
my $deg_to_radian= $pi/180;
print sin(30*$deg_to_radian),"\n";
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")
and
approx(".00000000001"+0)
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 |