| 1 | # -*- mode: perl; -*- |
| 2 | |
| 3 | use strictscip-perl cpan Math-BigInt 2.005003 strict/; |
| 4 | use warningsscip-perl cpan Math-BigInt 2.005003 warnings/; |
| 5 | |
| 6 | use Test::Morescip-perl cpan Math-BigInt 2.005003 Test/More/ tests => 32; |
| 7 | |
| 8 | use Math::BigIntscip-perl cpan Math-BigInt 2.005003 Math/BigInt/; |
| 9 | use Math::BigFloatscip-perl cpan Math-BigInt 2.005003 Math/BigFloat/; |
| 10 | |
| 11 | my @k = (16, 32, 64, 128); |
| 12 | |
| 13 | for my $k (@k) { |
| 14 | |
| 15 | # Parameters specific to this format: |
| 16 | |
| 17 | my $b = 2; |
| 18 | my $p = $k == 16 ? 11 |
| 19 | : $k == 32 ? 24 |
| 20 | : $k == 64 ? 53 |
| 21 | : $k - sprintfscip-perl cpan Math-BigInt 2.005003 main/sprintf().("%.0f", 4 * logscip-perl cpan Math-BigInt 2.005003 main/log().($k)/logscip-perl cpan Math-BigInt 2.005003 main/log().(2)) + 13; |
| 22 | |
| 23 | $b = Math::BigFloat -> newscip-perl cpan Math-BigInt 2.005003 Math/BigFloat/new().($b); |
| 24 | $k = Math::BigFloat -> newscip-perl cpan Math-BigInt 2.005003 Math/BigFloat/new().($k); |
| 25 | $p = Math::BigFloat -> newscip-perl cpan Math-BigInt 2.005003 Math/BigFloat/new().($p); |
| 26 | my $w = $k - $p; |
| 27 | |
| 28 | my $emax = 2 ** ($w - 1) - 1; |
| 29 | my $emin = 1 - $emax; |
| 30 | |
| 31 | my $format = 'binary' . $k; |
| 32 | |
| 33 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\nComputing test data for k = $k ...\n\n"); |
| 34 | |
| 35 | my $binv = Math::BigFloat -> newscip-perl cpan Math-BigInt 2.005003 Math/BigFloat/new().("0.5"); |
| 36 | |
| 37 | my $data = |
| 38 | [ |
| 39 | |
| 40 | { |
| 41 | dsc => "one", |
| 42 | bin => "0" |
| 43 | . "0" . ("1" x ($w - 1)) |
| 44 | . "0" x ($p - 1), |
| 45 | asc => "1", |
| 46 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("1"), |
| 47 | }, |
| 48 | |
| 49 | { |
| 50 | dsc => "minus one", |
| 51 | bin => "1" |
| 52 | . "0" . ("1" x ($w - 1)) |
| 53 | . "0" x ($p - 1), |
| 54 | asc => "-1", |
| 55 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("-1"), |
| 56 | }, |
| 57 | |
| 58 | { |
| 59 | dsc => "two", |
| 60 | bin => "0" |
| 61 | . "1" . ("0" x ($w - 1)) |
| 62 | . ("0" x ($p - 1)), |
| 63 | asc => "2", |
| 64 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("2"), |
| 65 | }, |
| 66 | |
| 67 | { |
| 68 | dsc => "minus two", |
| 69 | bin => "1" |
| 70 | . "1" . ("0" x ($w - 1)) |
| 71 | . ("0" x ($p - 1)), |
| 72 | asc => "-2", |
| 73 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("-2"), |
| 74 | }, |
| 75 | |
| 76 | { |
| 77 | dsc => "positive zero", |
| 78 | bin => "0" |
| 79 | . ("0" x $w) |
| 80 | . ("0" x ($p - 1)), |
| 81 | asc => "+0", |
| 82 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("0"), |
| 83 | }, |
| 84 | |
| 85 | { |
| 86 | dsc => "positive infinity", |
| 87 | bin => "0" |
| 88 | . ("1" x $w) |
| 89 | . ("0" x ($p - 1)), |
| 90 | asc => "+inf", |
| 91 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("inf"), |
| 92 | }, |
| 93 | |
| 94 | { |
| 95 | dsc => "negative infinity", |
| 96 | bin => "1" |
| 97 | . ("1" x $w) |
| 98 | . ("0" x ($p - 1)), |
| 99 | asc => "-inf", |
| 100 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("-inf"), |
| 101 | }, |
| 102 | |
| 103 | { |
| 104 | dsc => "NaN (encoding used by Perl on Cygwin)", |
| 105 | bin => "1" |
| 106 | . ("1" x $w) |
| 107 | . ("1" . ("0" x ($p - 2))), |
| 108 | asc => "NaN", |
| 109 | obj => Math::BigInt -> newscip-perl cpan Math-BigInt 2.005003 Math/BigInt/new().("NaN"), |
| 110 | }, |
| 111 | |
| 112 | ]; |
| 113 | |
| 114 | for my $entry (@$data) { |
| 115 | my $bin = $entry -> {bin}; |
| 116 | my $bytes = packscip-perl cpan Math-BigInt 2.005003 main/pack(). "B*", $bin; |
| 117 | my $hex = unpackscip-perl cpan Math-BigInt 2.005003 main/unpack(). "H*", $bytes; |
| 118 | |
| 119 | notescip-perl cpan Math-BigInt 2.005003 main/note().("\n", $entry -> {dsc}, " (k = $k): ", $entry -> {asc}, "\n\n"); |
| 120 | |
| 121 | my $x = $entry -> {obj}; |
| 122 | |
| 123 | my $test = qq|Math::BigInt -> new("$x") -> to_ieee754("$format")|; |
| 124 | |
| 125 | my $got_bytes = $x -> to_ieee754scip-perl cpan Math-BigInt 2.005003 main/to_ieee754().($format); |
| 126 | my $got_hex = unpackscip-perl cpan Math-BigInt 2.005003 main/unpack(). "H*", $got_bytes; |
| 127 | $got_hex =~ s/(..)/\\x$1/g; |
| 128 | |
| 129 | my $expected_hex = $hex; |
| 130 | $expected_hex =~ s/(..)/\\x$1/g; |
| 131 | |
| 132 | isscip-perl cpan Math-BigInt 2.005003 main/is().($got_hex, $expected_hex); |
| 133 | } |
| 134 | } |