This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
re-implement OPpASSIGN_COMMON mechanism
[perl5.git] / t / op / hexfp.t
CommitLineData
61e61fbc
JH
1#!./perl
2
61e61fbc
JH
3BEGIN {
4 chdir 't' if -d 't';
9224f6d1 5 @INC = '../lib';
61e61fbc 6 require './test.pl';
61e61fbc
JH
7}
8
0b1b7115
JH
9use strict;
10
11use Config;
12
61e61fbc
JH
13plan(tests => 79);
14
15# Test hexfloat literals.
16
17is(0x0p0, 0);
18is(0x0.p0, 0);
19is(0x.0p0, 0);
20is(0x0.0p0, 0);
21is(0x0.00p0, 0);
22
23is(0x1p0, 1);
24is(0x1.p0, 1);
25is(0x1.0p0, 1);
26is(0x1.00p0, 1);
27
28is(0x2p0, 2);
29is(0x2.p0, 2);
30is(0x2.0p0, 2);
31is(0x2.00p0, 2);
32
33is(0x1p1, 2);
34is(0x1.p1, 2);
35is(0x1.0p1, 2);
36is(0x1.00p1, 2);
37
38is(0x.1p0, 0.0625);
39is(0x0.1p0, 0.0625);
40is(0x0.10p0, 0.0625);
41is(0x0.100p0, 0.0625);
42
43# Positive exponents.
44is(0x1p2, 4);
45is(0x1p+2, 4);
46is(0x0p+0, 0);
47
48# Negative exponents.
49is(0x1p-1, 0.5);
50is(0x1.p-1, 0.5);
51is(0x1.0p-1, 0.5);
52is(0x0p-0, 0);
53
54is(0x1p+2, 4);
55is(0x1p-2, 0.25);
56
57is(0x3p+2, 12);
58is(0x3p-2, 0.75);
59
60# Shifting left.
61is(0x1p2, 1 << 2);
62is(0x1p3, 1 << 3);
63is(0x3p4, 3 << 4);
64is(0x3p5, 3 << 5);
65is(0x12p23, 0x12 << 23);
66
67# Shifting right.
68is(0x1p-2, 1 / (1 << 2));
69is(0x1p-3, 1 / (1 << 3));
70is(0x3p-4, 3 / (1 << 4));
71is(0x3p-5, 3 / (1 << 5));
72is(0x12p-23, 0x12 / (1 << 23));
73
74# Negative sign.
75is(-0x1p+2, -4);
76is(-0x1p-2, -0.25);
77is(-0x0p+0, 0);
78is(-0x0p-0, 0);
79
80is(0x0.10p0, 0.0625);
81is(0x0.1p0, 0.0625);
82is(0x.1p0, 0.0625);
83
84is(0x12p+3, 144);
85is(0x12p-3, 2.25);
86
87# Hexdigits (lowercase).
88is(0x9p+0, 9);
89is(0xap+0, 10);
90is(0xfp+0, 15);
91is(0x10p+0, 16);
92is(0x11p+0, 17);
93is(0xabp+0, 171);
94is(0xab.cdp+0, 171.80078125);
95
96# Uppercase hexdigits and exponent prefix.
97is(0xAp+0, 10);
98is(0xFp+0, 15);
99is(0xABP+0, 171);
100is(0xAB.CDP+0, 171.80078125);
101
102# Underbars.
103is(0xa_b.c_dp+1_2, 703696);
104
6c69a4c9
JH
105# Note that the hexfloat representation is not unique since the
106# exponent can be shifted, and the hexdigits with it: this is no
107# different from 3e4 cf 30e3 cf 30000. The shifting of the hexdigits
108# makes it look stranger, though: 0xap1 == 0x5p2.
61e61fbc
JH
109
110# Needs to use within() instead of is() because of long doubles.
6c69a4c9
JH
111within(0x1.99999999999ap-4, 0.1, 1e-9);
112within(0x3.333333333333p-5, 0.1, 1e-9);
113within(0xc.cccccccccccdp-7, 0.1, 1e-9);
61e61fbc
JH
114
115my $warn;
116
117local $SIG{__WARN__} = sub { $warn = shift };
118
119sub get_warn() {
120 my $save = $warn;
121 undef $warn;
122 return $save;
123}
124
125{ # Test certain things that are not hexfloats and should stay that way.
126 eval '0xp3';
127 like(get_warn(), qr/Missing operator before p3/);
128
129 eval '5p3';
130 like(get_warn(), qr/Missing operator before p3/);
131
132 my @a;
133 eval '@a = 0x3..5';
134 is("@a", "3 4 5");
135
136 eval '$a = eval "0x.3"';
137 is($a, '03');
138
139 eval '$a = eval "0xc.3"';
140 is($a, '123');
141}
142
143# Test warnings.
144SKIP:
145{
146 if ($Config{nv_preserves_uv_bits} == 53) {
147 local $^W = 1;
148
149 eval '0x1_0000_0000_0000_0p0';
150 is(get_warn(), undef);
151
152 eval '0x2_0000_0000_0000_0p0';
153 like(get_warn(), qr/^Hexadecimal float: mantissa overflow/);
154
155 eval '0x1.0000_0000_0000_0p0';
156 is(get_warn(), undef);
157
158 eval '0x2.0000_0000_0000_0p0';
159 like(get_warn(), qr/^Hexadecimal float: mantissa overflow/);
160
161 eval '0x.1p-1021';
162 is(get_warn(), undef);
163
164 eval '0x.1p-1023';
165 like(get_warn(), qr/^Hexadecimal float: exponent underflow/);
166
167 eval '0x1.fffffffffffffp+1023';
168 is(get_warn(), undef);
169
170 eval '0x1.fffffffffffffp+1024';
171 like(get_warn(), qr/^Hexadecimal float: exponent overflow/);
172 } else {
173 print "# skipping warning tests\n";
174 skip "nv_preserves_uv_bits is $Config{nv_preserves_uv_bits} not 53", 8;
175 }
176}
177
178# sprintf %a/%A testing is done in sprintf2.t,
179# trickier than necessary because of long doubles,
180# and because looseness of the spec.