Skip Menu |

This queue is for tickets about the Math-Complex CPAN distribution.

Report information
The Basics
Id: 32291
Status: resolved
Priority: 0/
Queue: Math-Complex

People
Owner: Nobody in particular
Requestors: sveshnik [...] fzu.cz
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Wrong (Math::Complex) results in great_circle family of functions
Date: Mon, 14 Jan 2008 13:11:17 +0100 (CET)
To: bug-Math-Complex [...] rt.cpan.org
From: sveshnik [...] fzu.cz
Hello, Problem: great_circle family of functions may produce wrong results on certain input. Affected versions: Math::Trig 1.44 and earlier. Test: $ perl -MMath::Trig=great_circle_distance -le 'print ref great_circle_distance(0,0.0002,0,0.0002)' Expected output: Empty string (since the result should be just a real number, zero). Output: Math::Complex Description: This bug is due to the fact, that cos(x)**2+sin(x)**2 is not always 1 in floating point calculations. For example, cos(0.0002)**2+sin(0.0002) is greater than 1 by approximately 2.22e-16 on x86 processors. In great_circle_distance function the following expression is used: acos(cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + sin( $lat0 ) * sin( $lat1 ) ) When $theta0 == $theta1 and $lat0 == $lat1 the expression is simplified to acos(cos($lat0)**2 + sin($lat0)**2) and for the certain $lat0 it gives numbers larger than 1. After that acos function returns a Math::Complex result. Suggested correction: - return $rho * - acos(cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + - sin( $lat0 ) * sin( $lat1 ) ); + $arg = cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + + sin( $lat0 ) * sin( $lat1 ); + return 0 if $arg > 1; + return $rho*atan2(1,1)*4 if $arg < -1; + return $rho*acos($arg); There is a similar problem with acos/asin usage in other great_circle functions. Hope this helps, Alexej Sveshnikov
Show quoted text
> Suggested correction: > > - return $rho * > - acos(cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + > - sin( $lat0 ) * sin( $lat1 ) ); > + $arg = cos( $lat0 ) * cos( $lat1 ) * cos( $theta0 - $theta1 ) + > + sin( $lat0 ) * sin( $lat1 ); > + return 0 if $arg > 1; > + return $rho*atan2(1,1)*4 if $arg < -1; > + return $rho*acos($arg); > > There is a similar problem with acos/asin usage in other great_circle > functions.
Thanks for catching this problem! Not to sound too greedy but could you work out similar corrections for the other great_circle functions? Show quoted text
> Hope this helps, > Alexej Sveshnikov >
Subject: Re: [rt.cpan.org #32291] Wrong (Math::Complex) results in great_circle family of functions
Date: Tue, 15 Jan 2008 10:45:10 +0100 (CET)
To: bug-Math-Complex [...] rt.cpan.org
From: sveshnik [...] fzu.cz
Show quoted text
> Not to sound too greedy but could you work out similar corrections for the > other great_circle > functions?
The patch below adds two new functions: real_acos and real_asin, which always return a real number. Other functions in the module are modified to use them instead of original Math::Complex versions. This should correct the problem in great_circle family of functions. Also, cartesian_to_spherical function had the same problem, since $rho=sqrt($x*$x+$y*$y+$z*$z) could be less than $z for very small values of $z. This patch corrects this problem too.

Message body is not shown because sender requested not to inline it.

Subject: Re: [rt.cpan.org #32291] Wrong (Math::Complex) results in great_circle family of functions
Date: Tue, 15 Jan 2008 21:35:31 -0500
To: bug-Math-Complex [...] rt.cpan.org
From: Jarkko Hietaniemi <jhi [...] iki.fi>
sveshnik@fzu.cz via RT wrote: Show quoted text
> Queue: Math-Complex > Ticket <URL: http://rt.cpan.org/Ticket/Display.html?id=32291 > >
>> Not to sound too greedy but could you work out similar corrections for the >> other great_circle >> functions?
> > The patch below adds two new functions: real_acos and real_asin, which > always return a real number. Other functions in the module are modified > to use them instead of original Math::Complex versions. This should > correct the problem in great_circle family of functions. > > Also, cartesian_to_spherical function had the same problem, since > $rho=sqrt($x*$x+$y*$y+$z*$z) could be less than $z for very small > values of $z. This patch corrects this problem too. >
Many thanks! Math-Complex 1.45 uploaded to CPAN.
Subject: Re: [rt.cpan.org #32291] Wrong (Math::Complex) results in great_circle family of functions
Date: Wed, 16 Jan 2008 08:56:13 +0100 (CET)
To: bug-Math-Complex [...] rt.cpan.org
From: sveshnik [...] fzu.cz
Show quoted text
>> The patch below adds two new functions: real_acos and real_asin, which >> always return a real number. Other functions in the module are modified >> to use them instead of original Math::Complex versions. This should >> correct the problem in great_circle family of functions. >> >> Also, cartesian_to_spherical function had the same problem, since >> $rho=sqrt($x*$x+$y*$y+$z*$z) could be less than $z for very small >> values of $z. This patch corrects this problem too. >>
> > Many thanks! Math-Complex 1.45 uploaded to CPAN. >
There is a typo in POD-documentation in Math::Trig 1.45 concerning real-valued asin and acos functions. The example code 'print cos(1e-6)**2+sin(1e-6)' should not print 1, because sine is not squared here. However, 'print cos(1e-6)**2+sin(1e-6)**2' prints just 1 because of the limited number of decimal places in the output. Suggested example: printf "%.20f", cos(1e-6)**2+sin(1e-6)**2 or print cos(1e-6)**2+sin(1e-6)**2-1
Resolved by 1.45 and 1.46.