[ANNOUNCE] Test::Float -- compare numbers to specified precision

From: David Golden (dg-junk_at_hyperbolic.net)
Date: 12/26/04


Date: Sun, 26 Dec 2004 15:20:58 -0600

NAME
    Test::Float - Compare if two numbers are equal within a specified
    precision

SYNOPSIS
      # Default precision
      use Test::Float;
      cmp_float( 1e-5, 2e-5, 'values within 1e-6'); # not ok
  
      # Specific precision
      use Test::Float within => 1e-5;
      cmp_float( 1e-5, 2e-5, 'values within 1e-5'); # ok

DESCRIPTION
    Most programmers at one time or another are confronted with the issue of
    comparing floating-point numbers for equality. The canonical idiom is to
    test if the absolute value of the difference of the numbers is within a
    desired precision. This module provides such a function for use with
    Test::Harness. Usage is similar to other test functions described in
    Test::More. Semantically, the "cmp_float" function replaces this kind of
    construct:

     ok ( abs($p - $q) <= $precision, '$p is equal to $q' ) or
         diag "$p is not equal to $q to within $precision";

    While there's nothing wrong with that construct, it's a pain to type it
    repeatedly in a test script. This module does the same thing with a
    single function call.

USAGE
  Importing
    By default, "use Test::Float" will compare equality with a precision of
    1e-6. (An arbitrary choice on my part.) To specific a different
    precision, provide a "within" parameter when importing the module with
    "use":

     use Test::Float within => 1e-9;

  cmp_float
     cmp_float( $p, $q, '$p and $q are equal' );

    This test compares equality within the precision specified during import
    (or the default of 1e-6). The test is true if the absolute value of the
    difference between $p and $q is less than or equal to the precision. If
    the test is true, it prints an "OK" statement for use in testing. If the
    test is not true, this function prints a failure report and diagnostic.

BUGS
    Please report bugs using the CPAN Request Tracker at
    http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Float

AUTHOR
     David A. Golden (DAGOLDEN)
     david@dagolden.com
     http://dagolden.com/

COPYRIGHT
    Copyright (c) 2004 by David A. Golden

    This program is free software; you can redistribute it and/or modify it
    under the same terms as Perl itself.

    The full text of the license can be found in the LICENSE file included
    with this module.

SEE ALSO
    Test::More, Test::Harness, Test::Builder



Relevant Pages

  • Re: Are there algorithm differences between 4.2.4 and 4.2.5?
    ... David J Taylor has hit on an apparent regression in timekeeping ... QPC builds with new Windows interpolation code, ... interpolation on Vista machines where the native clock precision is ...
    (comp.protocols.time.ntp)
  • Re: on-line calculator
    ... David W. Cantrell wrote: ...
    (sci.math)
  • Re: "Proper functions", Replacement and Zorns Lemma.
    ... David C. Ullrich writes: ... You said you don't need replacement, ... The "precision" you worried about earlier is of any interest only in ...
    (sci.logic)
  • Re: Truncate Milliseconds
    ... "David" wrote in message ... So far go good in test mode. ... Will put in production today so we will see. ... Finally found a KB that addresses the precision issue for dates in Access, ...
    (microsoft.public.vb.general.discussion)