xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/base/num.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateprint "1..50\n";
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate# First test whether the number stringification works okay.
6*0Sstevel@tonic-gate# (Testing with == would exercize the IV/NV part, not the PV.)
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate$a = 1; "$a";
9*0Sstevel@tonic-gateprint $a eq "1"       ? "ok 1\n"  : "not ok 1 # $a\n";
10*0Sstevel@tonic-gate
11*0Sstevel@tonic-gate$a = -1; "$a";
12*0Sstevel@tonic-gateprint $a eq "-1"      ? "ok 2\n"  : "not ok 2 # $a\n";
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate$a = 1.; "$a";
15*0Sstevel@tonic-gateprint $a eq "1"       ? "ok 3\n"  : "not ok 3 # $a\n";
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate$a = -1.; "$a";
18*0Sstevel@tonic-gateprint $a eq "-1"      ? "ok 4\n"  : "not ok 4 # $a\n";
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate$a = 0.1; "$a";
21*0Sstevel@tonic-gateprint $a eq "0.1"     ? "ok 5\n"  : "not ok 5 # $a\n";
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate$a = -0.1; "$a";
24*0Sstevel@tonic-gateprint $a eq "-0.1"    ? "ok 6\n"  : "not ok 6 # $a\n";
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate$a = .1; "$a";
27*0Sstevel@tonic-gateprint $a eq "0.1"     ? "ok 7\n"  : "not ok 7 # $a\n";
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate$a = -.1; "$a";
30*0Sstevel@tonic-gateprint $a eq "-0.1"    ? "ok 8\n"  : "not ok 8 # $a\n";
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gate$a = 10.01; "$a";
33*0Sstevel@tonic-gateprint $a eq "10.01"   ? "ok 9\n"  : "not ok 9 # $a\n";
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate$a = 1e3; "$a";
36*0Sstevel@tonic-gateprint $a eq "1000"    ? "ok 10\n" : "not ok 10 # $a\n";
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate$a = 10.01e3; "$a";
39*0Sstevel@tonic-gateprint $a eq "10010"   ? "ok 11\n"  : "not ok 11 # $a\n";
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate$a = 0b100; "$a";
42*0Sstevel@tonic-gateprint $a eq "4"       ? "ok 12\n"  : "not ok 12 # $a\n";
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate$a = 0100; "$a";
45*0Sstevel@tonic-gateprint $a eq "64"      ? "ok 13\n"  : "not ok 13 # $a\n";
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate$a = 0x100; "$a";
48*0Sstevel@tonic-gateprint $a eq "256"     ? "ok 14\n" : "not ok 14 # $a\n";
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate$a = 1000; "$a";
51*0Sstevel@tonic-gateprint $a eq "1000"    ? "ok 15\n" : "not ok 15 # $a\n";
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate# Okay, now test the numerics.
54*0Sstevel@tonic-gate# We may be assuming too much, given the painfully well-known floating
55*0Sstevel@tonic-gate# point sloppiness, but the following are still quite reasonable
56*0Sstevel@tonic-gate# assumptions which if not working would confuse people quite badly.
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate$a = 1; "$a"; # Keep the stringification as a potential troublemaker.
59*0Sstevel@tonic-gateprint $a + 1 == 2     ? "ok 16\n" : "not ok 16 #" . $a + 1 . "\n";
60*0Sstevel@tonic-gate# Don't know how useful printing the stringification of $a + 1 really is.
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate$a = -1; "$a";
63*0Sstevel@tonic-gateprint $a + 1 == 0     ? "ok 17\n" : "not ok 17 #" . $a + 1 . "\n";
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate$a = 1.; "$a";
66*0Sstevel@tonic-gateprint $a + 1 == 2     ? "ok 18\n" : "not ok 18 #" . $a + 1 . "\n";
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate$a = -1.; "$a";
69*0Sstevel@tonic-gateprint $a + 1 == 0     ? "ok 19\n" : "not ok 19 #" . $a + 1 . "\n";
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gatesub ok { # Can't assume too much of floating point numbers.
72*0Sstevel@tonic-gate    my ($a, $b, $c) = @_;
73*0Sstevel@tonic-gate    abs($a - $b) <= $c;
74*0Sstevel@tonic-gate}
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate$a = 0.1; "$a";
77*0Sstevel@tonic-gateprint ok($a + 1,  1.1,  0.05)   ? "ok 20\n" : "not ok 20 #" . $a + 1 . "\n";
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate$a = -0.1; "$a";
80*0Sstevel@tonic-gateprint ok($a + 1,  0.9,  0.05)   ? "ok 21\n" : "not ok 21 #" . $a + 1 . "\n";
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate$a = .1; "$a";
83*0Sstevel@tonic-gateprint ok($a + 1,  1.1,  0.005)  ? "ok 22\n" : "not ok 22 #" . $a + 1 . "\n";
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate$a = -.1; "$a";
86*0Sstevel@tonic-gateprint ok($a + 1,  0.9,  0.05)   ? "ok 23\n" : "not ok 23 #" . $a + 1 . "\n";
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate$a = 10.01; "$a";
89*0Sstevel@tonic-gateprint ok($a + 1, 11.01, 0.005) ? "ok 24\n" : "not ok 24 #" . $a + 1 . "\n";
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate$a = 1e3; "$a";
92*0Sstevel@tonic-gateprint $a + 1 == 1001  ? "ok 25\n" : "not ok 25 #" . $a + 1 . "\n";
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate$a = 10.01e3; "$a";
95*0Sstevel@tonic-gateprint $a + 1 == 10011 ? "ok 26\n" : "not ok 26 #" . $a + 1 . "\n";
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate$a = 0b100; "$a";
98*0Sstevel@tonic-gateprint $a + 1 == 0b101 ? "ok 27\n" : "not ok 27 #" . $a + 1 . "\n";
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate$a = 0100; "$a";
101*0Sstevel@tonic-gateprint $a + 1 == 0101  ? "ok 28\n" : "not ok 28 #" . $a + 1 . "\n";
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate$a = 0x100; "$a";
104*0Sstevel@tonic-gateprint $a + 1 == 0x101 ? "ok 29\n" : "not ok 29 #" . $a + 1 . "\n";
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate$a = 1000; "$a";
107*0Sstevel@tonic-gateprint $a + 1 == 1001  ? "ok 30\n" : "not ok 30 #" . $a + 1 . "\n";
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate# back to some basic stringify tests
110*0Sstevel@tonic-gate# we expect NV stringification to work according to C sprintf %.*g rules
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gateif ($^O eq 'os2') { # In the long run, fix this.  For 5.8.0, deal.
113*0Sstevel@tonic-gate    $a = 0.01; "$a";
114*0Sstevel@tonic-gate    print $a eq "0.01"   || $a eq '1e-02' ? "ok 31\n" : "not ok 31 # $a\n";
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate    $a = 0.001; "$a";
117*0Sstevel@tonic-gate    print $a eq "0.001"  || $a eq '1e-03' ? "ok 32\n" : "not ok 32 # $a\n";
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate    $a = 0.0001; "$a";
120*0Sstevel@tonic-gate    print $a eq "0.0001" || $a eq '1e-04' ? "ok 33\n" : "not ok 33 # $a\n";
121*0Sstevel@tonic-gate} else {
122*0Sstevel@tonic-gate    $a = 0.01; "$a";
123*0Sstevel@tonic-gate    print $a eq "0.01"    ? "ok 31\n" : "not ok 31 # $a\n";
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate    $a = 0.001; "$a";
126*0Sstevel@tonic-gate    print $a eq "0.001"   ? "ok 32\n" : "not ok 32 # $a\n";
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate    $a = 0.0001; "$a";
129*0Sstevel@tonic-gate    print $a eq "0.0001"  ? "ok 33\n" : "not ok 33 # $a\n";
130*0Sstevel@tonic-gate}
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate$a = 0.00009; "$a";
133*0Sstevel@tonic-gateprint $a eq "9e-05" || $a eq "9e-005" ? "ok 34\n"  : "not ok 34 # $a\n";
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate$a = 1.1; "$a";
136*0Sstevel@tonic-gateprint $a eq "1.1"     ? "ok 35\n" : "not ok 35 # $a\n";
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate$a = 1.01; "$a";
139*0Sstevel@tonic-gateprint $a eq "1.01"    ? "ok 36\n" : "not ok 36 # $a\n";
140*0Sstevel@tonic-gate
141*0Sstevel@tonic-gate$a = 1.001; "$a";
142*0Sstevel@tonic-gateprint $a eq "1.001"   ? "ok 37\n" : "not ok 37 # $a\n";
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate$a = 1.0001; "$a";
145*0Sstevel@tonic-gateprint $a eq "1.0001"  ? "ok 38\n" : "not ok 38 # $a\n";
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate$a = 1.00001; "$a";
148*0Sstevel@tonic-gateprint $a eq "1.00001" ? "ok 39\n" : "not ok 39 # $a\n";
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate$a = 1.000001; "$a";
151*0Sstevel@tonic-gateprint $a eq "1.000001" ? "ok 40\n" : "not ok 40 # $a\n";
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate$a = 0.; "$a";
154*0Sstevel@tonic-gateprint $a eq "0"       ? "ok 41\n" : "not ok 41 # $a\n";
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate$a = 100000.; "$a";
157*0Sstevel@tonic-gateprint $a eq "100000"  ? "ok 42\n" : "not ok 42 # $a\n";
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate$a = -100000.; "$a";
160*0Sstevel@tonic-gateprint $a eq "-100000" ? "ok 43\n" : "not ok 43 # $a\n";
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate$a = 123.456; "$a";
163*0Sstevel@tonic-gateprint $a eq "123.456" ? "ok 44\n" : "not ok 44 # $a\n";
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate$a = 1e34; "$a";
166*0Sstevel@tonic-gateunless ($^O eq 'posix-bc')
167*0Sstevel@tonic-gate{ print $a eq "1e+34" || $a eq "1e+034" ? "ok 45\n" : "not ok 45 $a\n"; }
168*0Sstevel@tonic-gateelse
169*0Sstevel@tonic-gate{ print "ok 45 # skipped on $^O\n"; }
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate# see bug #15073
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate$a = 0.00049999999999999999999999999999999999999;
174*0Sstevel@tonic-gate$b = 0.0005000000000000000104;
175*0Sstevel@tonic-gateprint $a <= $b ? "ok 46\n" : "not ok 46\n";
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gateif ($^O eq 'ultrix' || $^O eq 'VMS') {
178*0Sstevel@tonic-gate  # Ultrix enters looong nirvana over this. VMS blows up when configured with
179*0Sstevel@tonic-gate  # D_FLOAT (but with G_FLOAT or IEEE works fine).  The test should probably
180*0Sstevel@tonic-gate  # make the number of 0's a function of NV_DIG, but that's not in Config and
181*0Sstevel@tonic-gate  # we probably don't want to suck Config into a base test anyway.
182*0Sstevel@tonic-gate  print "ok 47\n";
183*0Sstevel@tonic-gate} else {
184*0Sstevel@tonic-gate  $a = 0.00000000000000000000000000000000000000000000000000000000000000000001;
185*0Sstevel@tonic-gate  print $a > 0 ? "ok 47\n" : "not ok 47\n";
186*0Sstevel@tonic-gate}
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate$a = 80000.0000000000000000000000000;
189*0Sstevel@tonic-gateprint $a == 80000.0 ? "ok 48\n" : "not ok 48\n";
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate$a = 1.0000000000000000000000000000000000000000000000000000000000000000000e1;
192*0Sstevel@tonic-gateprint $a == 10.0 ? "ok 49\n" : "not ok 49\n";
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate# From Math/Trig - number has to be long enough to exceed at least DBL_DIG
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate$a = 57.295779513082320876798154814169;
197*0Sstevel@tonic-gateprint ok($a*10,572.95779513082320876798154814169,1e-10) ? "ok 50\n" :
198*0Sstevel@tonic-gate  "not ok 50 # $a\n";
199