NAME

Test::Approximate -- compare two number for approximate equality, deeply

SYNOPSIS

use Test::Approximate;

is_approx(1, 1.0001, 'approximate equal', '1%');
is_approx(0.0001001, '1e-04', 'str vs num', '1%');
is_approx(1000, 1000.01, 'absolute tolerance', '0.1');


use Test::Deep;
use Test::Approximate;

$got = [ 1.00001, 2, 3, 4 ];
$expect = [ 1, 2, 3, 4 ];
cmp_deeply($got, approx($expect, '1%'), 'array');

$got = { a => 1, b => 1e-3, c => [ 1.1, 2.5, 5, 1e-9 ] };
$expect = { a => 1.0001, b => 1e-03, c => [ 1.1, 2.5, 5, 1.00001e-9 ] };
cmp_deeply( $got, approx($expect, '0.01%'), 'hash mix array');

DESCRIPTION

This module can test two scalar string or number numberic approximate equal, and deeply test two array or hash or array of hash etc.

There is already a nice module do this -- Test::Approx. I wrote this one because Test::Approx can't do a deeply test, and I have not found a module do the same thing.

FUNCTIONS

is_approx($got, $expected, [$msg, $tolerance])

Test $got and $expected 's difference.

This function is partly borrowed from Test::Approx, without the string Levenshtein difference. Only do a numeric difference;

$test_name defaults to 'got' =~ 'expected'

$tolerance is used to determine how different the scalars can be, it defaults to 1%. It can also be set as a number representing a threshold. To determine which:

$tolerance = '6%'; # threshold = calculated at 6%
$tolerance = 0.06; # threshold = 0.06
approx($aoh, $tolerance)

This function is used to do a deelpy approximate test, with Test::Deep

cmp_deeply($got, approx($expected, '1%'), 'test msg')

EXPORTS

is_approx, approx

AUTHOR

tadegenban <tadegenban@gmail.com>

COPYRIGHT

Copyright (c) 2014 tadegenban. Released under the same terms as Perl itself.

SEE ALSO

Text::Approx, Test::Builder, Test::Deep::Between,