Adam Turoff on Fri, 20 Jun 2003 16:09:28 -0400


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Floating point roundoff in cpan test scripts


On Fri, Jun 20, 2003 at 03:50:49PM -0400, Walt Mankowski wrote:
> On Fri, Jun 20, 2003 at 03:47:42PM -0400, Adam Turoff wrote:
> > > I'm using Test::More to do the tests.  The line that fails is
> > > 
> > >   is_deeply( [decimal2dms(  0.25416666666666667 )], [ 0, 15, 15]);
> > > 
> > > The error it's returning is
> > > 
> > > #     Structures begin differing at:
> > > #          $got->[2] = '15.0000000000001'
> > > #     $expected->[2] = '15'
> > > 
> > > Is there any easy way I can tell it that if it's within a given delta
> > > of the value I'm looking for, it's close enough?
> > 
> > Um, it looks like you're trying to convert a decimal to degrees, minutes
> > and seconds.  By definition, if you're going d/m/s, the first two values
> > should be integers.  There's no point in having a fractional minute and
> > also calculating seconds...
> > 
> > Pass the first two results through int() and see if that helps.  :-)
> 
> Oh, sorry, I guess that example is a little confusing, isn't it?
> 
> The arrays are indexed starting at 0, so it's complaining about the
> seconds, not the minutes.

D'Oh!  My brain is fried.

How many significant digits do you need?

	$minutes =~ m/((\d+)(\.\d{1,3})?)/;

Just return $2 if $3 is less than epsilon (or equal to zero).  Or return
$minutes or $1 if $3 is significant.

Z.

-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**