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   360540 use strict;
  4         7  
  4         118  
4 4     4   14 use warnings;
  4         16  
  4         208  
5 4     4   1705 use POSIX ();
  4         27896  
  4         240  
6              
7             our $VERSION = '1.02';
8              
9 4         32 use overload fallback => 1,
10             '""' => \&_num,
11             '*' => \&_multiply,
12 4     4   1997 '/' => \&_divide;
  4         5427  
13              
14             our $precision = .01;
15             our $offset = 0.5555555;
16              
17             sub import {
18 4 100   4   37 $precision = $_[1] if $_[1];
19 4 100       9 $offset = $_[2] if $_[2];
20 4         11 overload::constant integer => \&_smallnum;
21 4         95 overload::constant float => \&_smallnum;
22 4         62 overload::constant binary => \&_smallnum;
23             }
24              
25             sub _smallnum {
26 219     219   385 my $n = shift;
27 219         2708 bless \$n, __PACKAGE__;
28             }
29              
30             sub _num {
31 187     187   545199 my $num = _sref($_[0]);
32 187 100       4011 return $num >= 0
33             ? $precision * int(($num + ($offset * $precision)) / $precision)
34             : $precision * POSIX::ceil(($num - $offset * $precision) / $precision);
35             }
36              
37             sub _divide {
38 13     13   29 my (@ot) = (_sref($_[0]), _sref($_[1]));
39 13 100 100     125 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 8     8   18 my (@ot) = (_sref($_[0]), _sref($_[1]));
46 8         20 return _smallnum($ot[1] * $ot[0]);
47             }
48              
49 229 100   229   342 sub _sref { ref $_[0] ? ${$_[0]} : $_[0] }
  225         428  
50              
51             =head1 NAME
52              
53             smallnum - Transparent "SmallNumber" support for Perl
54              
55             =head1 VERSION
56              
57             Version 1.02
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 * Search CPAN
113              
114             L
115              
116             =back
117              
118             =head1 ACKNOWLEDGEMENTS
119              
120             =head1 LICENSE AND COPYRIGHT
121              
122             This software is Copyright (c) 2020->2025 by LNATION.
123              
124             This is free software, licensed under:
125              
126             The Artistic License 2.0 (GPL Compatible)
127              
128             =cut
129              
130             1; # End of smallnum