Subject: | Divide-by-zero error in q->power |
When $q->isreal, then $q->power($some_real_number) throws "Illegal division by zero".
The bug can be reproduced by:
$ perl -MMath::Quaternion -wE 'my $q = Math::Quaternion->new(-9,0,0,0); say $q * $q; say $q->power(2);'
( 81 0 0 0 )
Illegal division by zero at .../Math/Quaternion.pm line 527.
This is due to the interaction of any real quaternion, and this code in sub power():
my $vecmod = sqrt($a1*$a1+$a2*$a2+$a3*$a3);
my $stob = ($s**$b);
my $coeff = $stob/$vecmod*sin($b*$theta);
When $q->isreal, then $vecmod will always be zero, and the calculation of $coeff will always cause a divide-by-zero error.
The bug can be fixed by adding code just below the `if (ref $b)` block:
# For real_quaternion^real_number, use built-in power.
if ($a->isreal) {
return Math::Quaternion->new( $a->[0] ** $b, 0, 0, 0 ) ;
}
Other info:
$ uname -a
Darwin bg 12.4.0 Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
( OS X Mountain Lion )
$ perl -v
This is perl 5, version 18, subversion 0 (v5.18.0) built for darwin-2level
BTW, I found this bug while preparing a test harness to compare the output of
your Perl 5 Math::Quaternion module with my Perl 6 version:
https://github.com/Util/Perl6-Math-Quaternion
I have coded the fix, and added tests which fail without the fix, and pass with the fix.
Patch to follow momentarily.
--
Thanks for your open-source work!
Bruce Gray (Util on IRC and PerlMonks)