use strict; use warnings; use Math::MPFR qw(:mpfr); # Set precision to precisely 107 bits Rmpfr_set_default_prec(107); # Set $bin_str to the 107-bit representation of # 3.1415926535897932384626433832795 my $bin_str = '11001001000011111101101010100010001000010110100011000010001101001100010011000110011000101000101110000000111'; my $check = Math::MPFR->new(0); for(my $i = 0; $i < 107; $i++) { if(substr($bin_str, $i, 1)) { print "$i ", Math::MPFR->new(2 ** (-($i + 1))), "\n"; $check += 2 ** (-($i + 1)); } } print "\nSum =\n$check\nSum * 4 =\n", $check * 4, "\n";