Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created January 21, 2016 19:14
Show Gist options
  • Save p120ph37/efbcec1f74cacd0fe791 to your computer and use it in GitHub Desktop.
Save p120ph37/efbcec1f74cacd0fe791 to your computer and use it in GitHub Desktop.
Demonstrates machine epsilon in Perl
#!/usr/bin/perl
use warnings;
use strict;
my $val1;
if(($ARGV[0] // '') eq 'fixed') {
require Math::FixedPoint;
$val1 = Math::FixedPoint->new(2.1, 2); # fixed-point number
} else {
$val1 = 2.1; # floating point number
}
while($val1 * 10 == $val2) {
$val1 += 0.1;
$val2 += 1;
}
printf "Epsilon exceeded at (\$val1 = %0.16f) * 10 != (\$val2 = %d)\n", $val1, $val2;
@p120ph37
Copy link
Author

Example:

$ perl epsilon.pl
Epsilon exceeded at ($val1 = 2.3000000000000003) * 10 != ($val2 = 23)
$ perl epsilon.pl fixed
^C

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment