Skip to content

Commit

Permalink
Added function convenience function is_similar($x,$y,$eps) .
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Leto committed Apr 27, 2008
1 parent 1617aca commit 51f0f74
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/Math/GSL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ use constant MIN_DOUBLE => 2.2250738585072014e-308;
use constant MAX_FLOAT => 3.40282347e+38;
use constant MIN_FLOAT => 1.175494351e-38;


sub is_similar($$;$) {
my ($x,$y, $eps) = @_;
$eps ||= 1e-8;
abs($x-$y) < $eps ? 1 : 0;
}

sub is_valid_double
{
my $x=shift;
Expand All @@ -142,7 +149,7 @@ sub is_valid_double
$x < MAX_DOUBLE
) ? 1 : 0;
}
sub is_valid_float
sub is_valid_float
{
my $x=shift;
return 0 unless ( defined $x && looks_like_number($x) );
Expand Down
4 changes: 4 additions & 0 deletions t/GSL.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ my $results = {
q{is_valid_float(1e0)} => 1,
q{is_valid_float(1e0)} => 1,
q{is_valid_float('0e0')} => 1,
q{is_similar(5,5)} => 1,
q{is_similar(5,4)} => 0,
q{is_similar(0.10005,0.1000501, 1e-5)} => 1,
q{is_similar(0.10005,0.1000501, 1e-7)} => 0,
};

$gsl->verify_results($results);
Expand Down
2 changes: 1 addition & 1 deletion t/Poly.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ use warnings;

my $y = Math::GSL::Poly::gsl_poly_eval( $vals , 3, 1.0);

ok( abs($y - (3.14+2.72+5.55) ) < 1e-8, 'gsl_poly_eval' );
ok( Math::GSL::is_similar($y,3.14+2.72+5.55) , 'gsl_poly_eval' );
}

0 comments on commit 51f0f74

Please sign in to comment.