File Coverage

blib/lib/Math/Rational/Approx/ContFrac.pm
Criterion Covered Total %
statement 33 36 91.6
branch 2 2 100.0
condition n/a
subroutine 11 13 84.6
pod 3 3 100.0
total 49 54 90.7


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::ContFrac
6             #
7             # Math::Rational::Approx::ContFrac is free software: you can
8             # redistribute it and/or modify it under the terms of the GNU General
9             # Public License as published by the Free Software Foundation, either
10             # version 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::ContFrac;
23              
24 3     3   29094 use strict;
  3         12  
  3         130  
25 3     3   16 use warnings;
  3         5  
  3         88  
26 3     3   14 use Carp;
  3         12  
  3         381  
27              
28             our $VERSION = '0.01';
29              
30 3     3   5591 use Math::BigFloat;
  3         99435  
  3         20  
31              
32 3     3   140675 use Moo;
  3         77836  
  3         24  
33 3     3   9494 use MooX::Types::MooseLike::Numeric ':all';
  3         38036  
  3         942  
34              
35 3     3   13460 use Params::Validate qw[ validate_pos ARRAYREF ];
  3         41058  
  3         330  
36              
37 3     3   1905 use Math::Rational::Approx qw[ contfrac contfrac_nd ];
  3         13  
  3         2578  
38              
39             has x => (
40             is => 'ro',
41             isa => sub { die( "must be a positive number\n" )
42             unless is_PositiveNum($_[0]) },
43             required => 1,
44             );
45              
46             has n => (
47             is => 'rwp',
48             isa => PositiveInt,
49             required => 1,
50             );
51              
52              
53             has _terms => (
54             is => 'rwp',
55             init_arg => undef,
56             default => sub { [] },
57             );
58              
59 0     0 1 0 sub terms { [ @{$_[0]->_terms} ] }
  0         0  
60              
61             has _resid => (
62             is => 'rwp',
63             init_arg => undef,
64             lazy => 1,
65             builder => '_build_resid',
66             );
67              
68 0     0 1 0 sub resid { $_[0]->x->copy }
69              
70              
71 6     6   520 sub _build_resid { Math::BigFloat->new( $_[0]->x ) }
72              
73             sub approx {
74              
75 9     9 1 450 my $self = shift;
76              
77             my ( $n ) = validate_pos( @_,
78             { optional => 1,
79             callbacks => {
80 3     3   16 'positive integer' => sub { is_PositiveInt($_[0]) },
81             },
82 9         186 });
83              
84 9 100       236 $self->_set_n( $self->n + $n )
85             if defined $n;
86              
87 9         1172 my ( undef, $x ) = contfrac( $self->_resid, $self->n - @{$self->_terms}, $self->_terms );
  9         1408  
88 9         65 $self->_set__resid( $x );
89              
90 9         47 return contfrac_nd( $self->_terms );
91             }
92              
93             1;
94              
95              
96             __END__