| 1 | package Machine::Epsilon; | ||||||||||
| 2 | 1 | ▶ | use strict; use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | our @ISA = qw(Exporter); | |||||||||
| 6 | 1 | our @EXPORT = qw(machine_epsilon); | |||||||||
| 7 | |||||||||||
| 8 | 1 | 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 | 1 | my $epsilon; | |||||||||
| 38 | |||||||||||
| 39 | sub machine_epsilon { | ||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | return $epsilon if $epsilon; | ||||||||
|
Subroutine
machine_epsilon: called CC=5 SCAR=17.5
Pod
documented
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | # Machine accuracy for 32-bit floating point number | ||||||||||
| 44 | 1 | my $ma_32bit_23mantissa = 1.0 / ( 2**23 ); | |||||||||
| 45 | |||||||||||
| 46 | # Machine accuracy for 64-bit floating point number | ||||||||||
| 47 | 1 | my $ma_64bit_52mantissa = 1.0 / ( 2**52 ); | |||||||||
| 48 | |||||||||||
| 49 | # Machine accuracy for 128-bit floating point number (e.g. IBM AIX) | ||||||||||
| 50 | 1 | my $ma_128bit_105mantissa = 1.0 / ( 2**112 ); | |||||||||
| 51 | |||||||||||
| 52 | # Always start with a power of 2 to avoid roundoff errors!! | ||||||||||
| 53 | 1 | my $e = 1.0; | |||||||||
| 54 | 1 | while (1) { | |||||||||
| 55 | 53 | ▶ | if ( 1.0 + $e / 2 == 1.0 ) { last; } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 52 | $e = $e / 2.0; | |||||||||
| 57 | |||||||||||
| 58 | # Accuracy already better than a 128-bit machine!! | ||||||||||
| 59 | 52 | ▶ | if ( $e < $ma_128bit_105mantissa ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 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 | $e = $ma_64bit_52mantissa; | |||||||||
| 65 | 0 | last; | |||||||||
| 66 | } | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # If accuracy is very bad, we return the minimum accuracy for a 32-bit double | ||||||||||
| 70 | 1 | ▶ | if ( $e > $ma_32bit_23mantissa ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 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 | return $ma_32bit_23mantissa; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 1 | $epsilon = $e; | |||||||||
| 78 | |||||||||||
| 79 | 1 | return $e; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | =head1 AUTHOR | ||||||||||
| 83 | |||||||||||
| 84 | binary.com, C<< <perl at binary.com> >> | ||||||||||
| 85 | |||||||||||
| 86 | =head1 BUGS | ||||||||||
| 87 | |||||||||||
| 88 | Please report any bugs or feature requests to C<bug-machine-epsilon at rt.cpan.org>, or through | ||||||||||
| 89 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Machine-Epsilon>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Machine-Epsilon> | ||||||||||
| 107 | |||||||||||
| 108 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 109 | |||||||||||
| 110 | L<http://annocpan.org/dist/Machine-Epsilon> | ||||||||||
| 111 | |||||||||||
| 112 | =item * CPAN Ratings | ||||||||||
| 113 | |||||||||||
| 114 | L<http://cpanratings.perl.org/d/Machine-Epsilon> | ||||||||||
| 115 | |||||||||||
| 116 | =item * Search CPAN | ||||||||||
| 117 | |||||||||||
| 118 | L<http://search.cpan.org/dist/Machine-Epsilon/> | ||||||||||
| 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<http://dev.perl.org/licenses/> for more information. | ||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =cut | ||||||||||
| 138 | |||||||||||
| 139 | 1 | 1; # End of Machine::Epsilon | |||||||||
| 140 | |||||||||||