File Coverage

lib/Machine/Epsilon.pm
Criterion Covered Total %
statement 20 25 80.0
branch 4 6 66.6
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 28 36 77.7


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