File Coverage

blib/lib/Math/Rational/Approx/MaxD.pm
Criterion Covered Total %
statement 33 33 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 11 11 100.0
pod 1 2 50.0
total 51 53 96.2


line stmt bran cond sub pod time code
1             # --8<--8<--8<--8<--
2             #
3             # Copyright (C) 2012 Smithsonian Astrophysical Observatory
4             #
5             # This file is part of Math::Rational::Approx::MaxD
6             #
7             # Math::Rational::Approx::MaxD is free software: you can redistribute
8             # it and/or modify it under the terms of the GNU General Public
9             # License as published by the Free Software Foundation, either version
10             # 3 of the License, or (at your option) any later version.
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program. If not, see .
19             #
20             # -->8-->8-->8-->8--
21              
22             package Math::Rational::Approx::MaxD;
23              
24 3     3   4439 use strict;
  3         13  
  3         100  
25 3     3   13 use warnings;
  3         7  
  3         70  
26 3     3   16 use Carp;
  3         6  
  3         299  
27              
28              
29             our $VERSION = '0.01';
30              
31 3     3   1658 use Params::Validate qw[ validate_pos ARRAYREF ];
  3         19793  
  3         193  
32 3     3   1112 use Math::Rational::Approx;
  3         8  
  3         138  
33              
34 3     3   2088 use Moo;
  3         32778  
  3         20  
35 3     3   4533 use MooX::Types::MooseLike::Numeric ':all';
  3         8  
  3         769  
36 3     3   16 use MooX::Types::MooseLike::Base ':all';
  3         7  
  3         2393  
37              
38             has x => (
39             is => 'ro',
40             isa => PositiveNum,
41             required => 1,
42             );
43              
44             has maxD => (
45             is => 'rwp',
46             isa => PositiveInt,
47             required => 1,
48             );
49              
50             has bounds => (
51             is => 'ro',
52             coerce => sub { return is_ArrayRef($_[0]) ? [ @{$_[0]} ] : $_[0] },
53             isa => ArrayRef[PositiveOrZeroInt],
54             default => sub { [] },
55             );
56              
57             sub BUILD {
58              
59 18     18 0 3409 my $self = shift;
60              
61 18         251 Math::Rational::Approx::_check_bounds( $self->x, $self->bounds )
62 18 100       23 if @{$self->bounds};
63             }
64              
65              
66             sub approx {
67              
68 20     20 1 4156 my $self = shift;
69              
70             my ( $maxD ) = validate_pos( @_,
71 10     10   31 { callbacks => { 'positive integer' => sub { is_PositiveInt($_[0]) } },
72 20         260 optional => 1,
73             }
74             );
75 20 100 66     557 $self->_set_maxD( $maxD )
76             if defined $maxD && $maxD > $self->maxD;
77              
78 20         1151 my ( $n, $d ) = Math::Rational::Approx::maxD( $self->x, $self->maxD, $self->bounds );
79              
80 20         58 return ( $n, $d );
81             }
82              
83             1;
84              
85              
86              
87             __END__