File Coverage

lib/Machine/Epsilon.pm
Criterion Covered Total %
statement 22 27 81.4
branch 5 8 62.5
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 32 40 80.0


line stmt bran cond sub pod time code
1             package Machine::Epsilon;
2 1     1   458 use strict; use warnings;
  1     1   1  
  1         29  
  1         5  
  1         1  
  1         26  
3              
4 1     1   13 use Exporter;
  1         1  
  1         215  
5             our @ISA = qw(Exporter);
6             our @EXPORT = qw(machine_epsilon);
7              
8             our $VERSION = '1.0.1';
9              
10             =head1 NAME
11              
12             Machine::Epsilon - The maximum relative error while rounding a floating point number
13              
14             =head1 VERSION
15              
16             Version 1.00
17              
18             =cut
19              
20             =head1 SYNOPSIS
21              
22              
23             use Machine::Epsilon; # imports machine_epsilon() automatically
24              
25             my $epsilon = machine_epsilon();
26              
27             =cut
28              
29             =head1 FUNCTIONS
30              
31             =head2 machine_epsilon
32              
33             Returns the rounding error for the machine.
34              
35             =cut
36              
37             my $epsilon;
38              
39             sub machine_epsilon {
40              
41 1 50   1 1 364 return $epsilon if $epsilon;
42              
43             # Machine accuracy for 32-bit floating point number
44 1         2 my $ma_32bit_23mantissa = 1.0 / ( 2**23 );
45              
46             # Machine accuracy for 64-bit floating point number
47 1         2 my $ma_64bit_52mantissa = 1.0 / ( 2**52 );
48              
49             # Machine accuracy for 128-bit floating point number (e.g. IBM AIX)
50 1         1 my $ma_128bit_105mantissa = 1.0 / ( 2**112 );
51              
52             # Always start with a power of 2 to avoid roundoff errors!!
53 1         1 my $e = 1.0;
54 1         1 while (1) {
55 53 100       83 if ( 1.0 + $e / 2 == 1.0 ) { last; }
  1         3  
56 52         39 $e = $e / 2.0;
57              
58             # Accuracy already better than a 128-bit machine!!
59 52 50       76 if ( $e < $ma_128bit_105mantissa ) {
60 0         0 warn "Machine accuracy seems too good to be true!! "
61             . "Do we have such a powerful machine? Assuming that "
62             . "something isn't right, and returning machine accuracy "
63             . "for 64 bit double.";
64 0         0 $e = $ma_64bit_52mantissa;
65 0         0 last;
66             }
67             }
68              
69             # If accuracy is very bad, we return the minimum accuracy for a 32-bit double
70 1 50       3 if ( $e > $ma_32bit_23mantissa ) {
71 0         0 warn "Machine accuracy ($e greater than $ma_32bit_23mantissa) "
72             . "seems worse than the primitive 32-bit double representation. "
73             . "Setting to minimum accuracy of $ma_32bit_23mantissa.";
74 0         0 return $ma_32bit_23mantissa;
75             }
76              
77 1         1 $epsilon = $e;
78              
79 1         3 return $e;
80             }
81              
82             =head1 AUTHOR
83              
84             binary.com, C<< >>
85              
86             =head1 BUGS
87              
88             Please report any bugs or feature requests to C, or through
89             the web interface at L. I will be notified, and then you'll
90             automatically be notified of progress on your bug as I make changes.
91              
92              
93             =head1 SUPPORT
94              
95             You can find documentation for this module with the perldoc command.
96              
97             perldoc Machine::Epsilon
98              
99              
100             You can also look for information at:
101              
102             =over 4
103              
104             =item * RT: CPAN's request tracker (report bugs here)
105              
106             L
107              
108             =item * AnnoCPAN: Annotated CPAN documentation
109              
110             L
111              
112             =item * CPAN Ratings
113              
114             L
115              
116             =item * Search CPAN
117              
118             L
119              
120             =back
121              
122             =head1 REFERENCES
123              
124             http://en.wikipedia.org/wiki/Machine_epsilon
125              
126             =head1 LICENSE AND COPYRIGHT
127              
128             Copyright 2014 binary.com.
129              
130             This program is free software; you can redistribute it and/or modify it
131             under the terms of either: the GNU General Public License as published
132             by the Free Software Foundation; or the Artistic License.
133              
134             See L for more information.
135              
136              
137             =cut
138              
139             1; # End of Machine::Epsilon
140