File: doubletoa.t

package info (click to toggle)
libmath-mpfr-perl 4.45-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,716 kB
  • sloc: perl: 1,508; ansic: 517; makefile: 9
file content (174 lines) | stat: -rwxr-xr-x 4,504 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use strict;
use warnings;

use Math::MPFR qw(:mpfr);
use Config;

if($Config{nvsize} != 8) {

  print "1..2\n";

  if(Math::MPFR::_fallback_notify()) { print "not ok 1\n"}
  else {print "ok 1\n"}

  eval {my $x = doubletoa(42.0) };

  if($@ =~ /^The doubletoa function is unavailable/) { print "ok 2\n" }
  else {
    warn "\$\@: $@\n";
    print "not ok 2\n";
  }

}

elsif(MPFR_VERSION() <= 196869) {
  print "1..1\n";
  warn "\nSkipping all tests - they require mpfr-3.1.6 or later\n";
  print "ok 1\n";
}

else {

  print "1..11\n";

  my $ok = 1;
  my $fb = Math::MPFR::_fallback_notify();
  my $fb_tracker = 0;
  my ($count, $mismatch_count) = (0, 0);


  for my $iteration(1..1000) {
    last unless $ok;
    for my $exp(-326 .. 325) {
      $count++;
      my $str = rand(100);
      if($str !~ /e/) { $str .= (int(rand(10)) . int(rand(10)) . "e$exp") }
      $str = '-' . $str unless $iteration % 3;
      if($fb) { $fb_tracker = $Math::MPFR::doubletoa_fallback }
      my $v = $str + 0;
      my $s1 = doubletoa($v, "S");
      my $s2 = nvtoa($v);

      if($s1 ne $s2) {
        #print "$str $s1 $s2\n";
        $mismatch_count++;
        my $s1_alt = doubletoa($v);

        if($fb && $Math::MPFR::doubletoa_fallback - $fb_tracker != 2) {
          $ok = 0;
          warn "\nfallback anomaly with $str: $s1 ($s1_alt) $s2\n";
          last;
        }

        my ($check1, $check2, $check3) =  (
                                           ($s1 eq $s1_alt),
                                           (atonv($s1) != atonv($s1_alt)),
                                           (atonv($s1) != atonv($s2))
                                          );

        if($check1 || $check2 || $check3) {
          $ok = 0;
          warn "\nmismatch for $str: $s1 ($s1_alt) $s2\n";
          last;
        }
      }
    }
  }

#  print "Fallback: $Math::MPFR::doubletoa_fallback Mismatch: $mismatch_count\n";

  if($ok) { print "ok 1\n" }
  else { print "not ok 1\n" }

  if($fb) {
    if($count > 10000) {
      if($Math::MPFR::doubletoa_fallback > 10 && $count / $Math::MPFR::doubletoa_fallback > 50) {
        print "ok 2\n";
      }
      else {
        warn "\n  Total Count: $count\nFallback count: $Math::MPFR::doubletoa_fallback\n";
        print "not ok 2\n";
      }
    }
    else {
      warn "\n Skipping test 2 - didn't test enough values\n";
      print "ok 2\n";
    }
  }
  else {
    if($Math::MPFR::doubletoa_fallback) { print "not ok 2\n" }
    else { print "ok 2\n" }
  }

  if(doubletoa(atodouble('8e94')) eq '8e+94') { print "ok 3\n" }
  else {
    warn "\nexpected: '8e+94'\ngot     : '", doubletoa(atodouble('8e+94')), "'\n";
    print "not ok 3\n";
  }

  if(doubletoa(atodouble('-8e94')) eq '-8e+94') { print "ok 4\n" }
  else {
    warn "\nexpected: '-8e+94'\ngot     : '", doubletoa(atodouble('-8e+94')), "'\n";
    print "not ok 4\n";
  }

  if(doubletoa(atodouble('80e94')) eq '8e+95') { print "ok 5\n" }
  else {
    warn "\nexpected: '8e+95'\ngot     : '", doubletoa(atodouble('80e+94')), "'\n";
    print "not ok 5\n";
  }

  if(doubletoa(atodouble('81e94')) eq '8.1e+95') { print "ok 6\n" }
  else {
    warn "\nexpected: '8.1e+95'\ngot     : '", doubletoa(atodouble('81e+94')), "'\n";
    print "not ok 6\n";
  }

  if(doubletoa(atodouble('8000000e94')) eq '8e+100') { print "ok 7\n" }
  else {
    warn "\nexpected: '8e+100'\ngot     : '", doubletoa(atodouble('8000000e+94')), "'\n";
    print "not ok 7\n";
  }

  # 1e+23 is one of the values that Grisu3 cannot handle.

  my $d    = atodouble('1e+23');
  my $dtoa = doubletoa($d); # fall back to dragon

  if($dtoa eq '1e+23') { print "ok 8\n" }
  else {
    warn "\nexpected: '1e+23'\ngot     : '", $dtoa, "'\n";
    print "not ok 8\n";
  }

  $dtoa = doubletoa($d, ''); # fall back to sprintf("%.17g", $d)

  if($dtoa eq '9.9999999999999992e+22' || $dtoa eq '9.9999999999999992e+022') { print "ok 9\n" }
  else {
    warn "\nexpected: '9.9999999999999992e+22 or 9.9999999999999992e+022'\ngot     : '", $dtoa, "'\n";
    print "not ok 9\n";
  }

  $dtoa = doubletoa(0.0);

  if($dtoa eq '0.0') { print "ok 10\n" }
  else {
    warn "\nexpected: '0.0'\ngot     : '", $dtoa, "'\n";
    print "not ok 10\n";
  }

  my $z = Math::MPFR->new(0);
  Rmpfr_neg($z, $z, MPFR_RNDN); # $z is -0.0
  my $negzero = Rmpfr_get_NV($z, MPFR_RNDN);

  $dtoa = doubletoa($negzero);

  if($dtoa eq '-0.0') { print "ok 11\n" }
  else {
    warn "\nexpected: '-0.0'\ngot     : '", $dtoa, "'\n";
    print "not ok 11\n";
  }
}


__END__