File Coverage

blib/lib/smallnum.pm
Criterion Covered Total %
statement 27 27 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 10 10 100.0
pod n/a
total 52 52 100.0


line stmt bran cond sub pod time code
1             package smallnum;
2              
3 4     4   279176 use strict;
  4         32  
  4         120  
4 4     4   20 use warnings;
  4         6  
  4         95  
5 4     4   2225 use POSIX ();
  4         26370  
  4         286  
6              
7             our $VERSION = '1.00';
8              
9 4         72 use overload fallback => 1,
10             '""' => \&_num,
11             '*' => \&_multiply,
12 4     4   4680 '/' => \&_divide;
  4         3815  
13              
14             our $precision = .01;
15             our $offset = 0.5555555;
16              
17             sub import {
18 4 100   4   38 $precision = $_[1] if $_[1];
19 4 100       16 $offset = $_[2] if $_[2];
20 4         18 overload::constant integer => \&_smallnum;
21 4         103 overload::constant float => \&_smallnum;
22 4         60 overload::constant binary => \&_smallnum;
23             }
24              
25             sub _smallnum {
26 202     202   459 my $n = shift;
27 202         2678 bless \$n, __PACKAGE__;
28             }
29              
30             sub _num {
31 174     174   16478 my $num = _sref($_[0]);
32 174 100       4833 return $num >= 0
33             ? $precision * int(($num + ($offset * $precision)) / $precision)
34             : $precision * POSIX::ceil(($num - $offset * $precision) / $precision);
35             }
36              
37             sub _divide {
38 12     12   29 my (@ot) = (_sref($_[0]), _sref($_[1]));
39 12 100 100     91 return $ot[0] && $ot[1]
    100          
40             ? _smallnum($_[2] ? ($ot[1] / $ot[0]) : ($ot[0] / $ot[1]))
41             : _smallnum(0);
42             }
43              
44             sub _multiply {
45 7     7   16 my (@ot) = (_sref($_[0]), _sref($_[1]));
46 7         22 return _smallnum($ot[1] * $ot[0]);
47             }
48              
49 212 100   212   460 sub _sref { ref $_[0] ? ${$_[0]} : $_[0] }
  208         489  
50              
51             =head1 NAME
52              
53             smallnum - Transparent "SmallNumber" support for Perl
54              
55             =head1 VERSION
56              
57             Version 1.00
58              
59             =cut
60              
61             =head1 SYNOPSIS
62              
63             use smallnum;
64              
65             10 + 20.452433483 # 30.45
66             20.3743543 - 10.1 # 10.27
67             15 / 5.34, # 2.81
68             9 * 0.01, # 0.09
69              
70             ...
71            
72             use smallnum '0.1';
73              
74             10 + 20.452433483 # 30.5
75             20.3743543 - 10.1 # 10.3
76             15 / 5.34, # 2.8
77             9 * 0.01, # 0.1
78              
79             ...
80              
81             use smallnum '1';
82              
83             10 + 20.452433483 # 31
84             20.3743543 - 10.1 # 10
85             15 / 5.34, # 3
86             9 * 0.01, # 0
87              
88             =head1 AUTHOR
89              
90             LNATION, C<< >>
91              
92             =head1 BUGS
93              
94             Please report any bugs or feature requests to C, or through
95             the web interface at L. I will be notified, and then you'll
96             automatically be notified of progress on your bug as I make changes.
97              
98             =head1 SUPPORT
99              
100             You can find documentation for this module with the perldoc command.
101              
102             perldoc smallnum
103              
104             You can also look for information at:
105              
106             =over 4
107              
108             =item * RT: CPAN's request tracker (report bugs here)
109              
110             L
111              
112             =item * AnnoCPAN: Annotated CPAN documentation
113              
114             L
115              
116             =item * CPAN Ratings
117              
118             L
119              
120             =item * Search CPAN
121              
122             L
123              
124             =back
125              
126              
127             =head1 ACKNOWLEDGEMENTS
128              
129              
130             =head1 LICENSE AND COPYRIGHT
131              
132             This software is Copyright (c) 2020->2021 by LNATION.
133              
134             This is free software, licensed under:
135              
136             The Artistic License 2.0 (GPL Compatible)
137              
138              
139             =cut
140              
141             1; # End of smallnum