C:\_32\pscrpt\math-mpfr>perl sum.pl 0 5e-1 1 2.5e-1 4 3.125e-2 7 3.90625e-3 12 1.220703125e-4 13 6.103515625e-5 14 3.0517578125e-5 15 1.52587890625e-5 16 7.62939453125e-6 17 3.814697265625e-6 19 9.5367431640625e-7 20 4.76837158203125e-7 22 1.1920928955078125e-7 24 2.98023223876953125e-8 26 7.450580596923828125e-9 30 4.656612873077392578125e-10 34 2.910383045673370361328125e-11 39 9.094947017729282379150390625e-13 41 2.27373675443232059478759765625e-13 42 1.136868377216160297393798828125e-13 44 2.8421709430404007434844970703125e-14 48 1.776356839400250464677810668945312e-15 49 8.881784197001252323389053344726562e-16 54 2.775557561562891351059079170227051e-17 58 1.734723475976807094411924481391907e-18 59 8.673617379884035472059622406959534e-19 61 2.168404344971008868014905601739883e-19 64 2.710505431213761085018632002174854e-20 65 1.355252715606880542509316001087427e-20 69 8.47032947254300339068322500679642e-22 72 1.058791184067875423835403125849552e-22 73 5.293955920339377119177015629247762e-23 77 3.308722450212110699485634768279851e-24 78 1.654361225106055349742817384139926e-24 81 2.067951531382569187178521730174907e-25 82 1.033975765691284593589260865087454e-25 86 6.462348535570528709932880406796585e-27 88 1.615587133892632177483220101699146e-27 92 1.009741958682895110927012563561966e-28 94 2.524354896707237777317531408904916e-29 95 1.262177448353618888658765704452458e-29 96 6.31088724176809444329382852226229e-30 104 2.465190328815661891911651766508707e-32 105 1.232595164407830945955825883254353e-32 106 6.162975822039154729779129416271767e-33 Sum = 7.853981633974483096156608458198765e-1 Sum * 4 = 3.141592653589793238462643383279506 #### 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";