Skip to content

Commit 028761a

Browse files
Add is_representable_as_float() and is_representable_as_int
1 parent 16b2fc4 commit 028761a

File tree

6 files changed

+728
-1
lines changed

6 files changed

+728
-1
lines changed

ext/standard/basic_functions.stub.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,16 @@ function is_iterable(mixed $value): bool {}
37003700
*/
37013701
function is_countable(mixed $value): bool {}
37023702

3703+
/**
3704+
* @compile-time-eval
3705+
*/
3706+
function is_representable_as_float(mixed $value): bool {}
3707+
3708+
/**
3709+
* @compile-time-eval
3710+
*/
3711+
function is_representable_as_int(mixed $value): bool {}
3712+
37033713
/* uniqid.c */
37043714

37053715
#ifdef HAVE_GETTIMEOFDAY

ext/standard/basic_functions_arginfo.h

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
--TEST--
2+
Test is_representable_as_float() function
3+
--SKIPIF--
4+
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64-bit systems only"); ?>
5+
--FILE--
6+
<?php
7+
$representable_as_float = array(
8+
0,
9+
1,
10+
-1,
11+
42,
12+
-42,
13+
14+
9007199254740992, // 2^53
15+
-9007199254740992, // -2^53
16+
9007199254740991, // 2^53 - 1
17+
-9007199254740991, // -(2^53 - 1)
18+
19+
// from 2^53 to 2^54, only even numbers are exactly representable
20+
9007199254740994, // 2^53 + 2
21+
9007199254740996, // 2^53 + 4
22+
18014398509481984, // 2^54
23+
-9007199254740994, // -(2^53 + 2)
24+
25+
// from 2^54 to 2^55, only multiples of 4 are exactly representable
26+
18014398509481988, // 2^54 + 4
27+
18014398509481992, // 2^54 + 8
28+
36028797018963968, // 2^55
29+
30+
// from 2^55 to 2^56, only multiples of 8 are exactly representable
31+
36028797018963976, // 2^55 + 8
32+
36028797018963984, // 2^55 + 16
33+
72057594037927936, // 2^56
34+
35+
// pure powers of two
36+
1152921504606846976, // 2^60
37+
2305843009213693952, // 2^61
38+
39+
PHP_INT_MIN,
40+
41+
0.0,
42+
-0.0,
43+
1.0,
44+
-1.0,
45+
3.14,
46+
-3.14,
47+
48+
INF,
49+
-INF,
50+
NAN,
51+
52+
2,
53+
4,
54+
8,
55+
16,
56+
1024,
57+
58+
"0",
59+
"1",
60+
"-1",
61+
"42",
62+
"0.0",
63+
"1.0",
64+
"-1.0",
65+
"3.14",
66+
"-3.14",
67+
"1e10",
68+
"-1e10",
69+
"1.5e-10",
70+
"9007199254740992", // 2^53
71+
"-9007199254740992", // -2^53
72+
73+
74+
"9007199254740994", // 2^53 + 2
75+
"18014398509481988", // 2^54 + 4
76+
"36028797018963976", // 2^55 + 8
77+
78+
" 42 ",
79+
"\t1.5\n",
80+
"\r-3.14\v",
81+
);
82+
83+
foreach ($representable_as_float as $value) {
84+
var_dump(is_representable_as_float($value));
85+
}
86+
87+
$not_representable_as_float = array(
88+
9007199254740993, // 2^53 + 1
89+
-9007199254740993, // -(2^53 + 1)
90+
9007199254740995, // 2^53 + 3
91+
92+
// odd numbers in 2^53 to 2^54 range
93+
9007199254740995, // 2^53 + 3
94+
9007199254740997, // 2^53 + 5
95+
18014398509481985, // 2^54 + 1
96+
97+
// not multiples of 4 in 2^54 to 2^55 range
98+
18014398509481986, // 2^54 + 2
99+
18014398509481990, // 2^54 + 6
100+
36028797018963970, // 2^55 + 2
101+
102+
// not multiples of 8 in 2^55 to 2^56 range
103+
36028797018963972, // 2^55 + 4
104+
36028797018963978, // 2^55 + 10
105+
72057594037927940, // 2^56 + 4
106+
null,
107+
true,
108+
false,
109+
"",
110+
"hello",
111+
"123.456.789",
112+
"1e400",
113+
"-1e400",
114+
115+
[],
116+
[1, 2, 3],
117+
new stdClass(),
118+
119+
120+
"9007199254740993", // 2^53 + 1
121+
"18014398509481986", // 2^54 + 2 (not multiple of 4)
122+
"36028797018963972", // 2^55 + 4 (not multiple of 8)
123+
124+
"0x42",
125+
"0xFF",
126+
127+
"0b1010",
128+
);
129+
130+
$fp = fopen(__FILE__, "r");
131+
$not_representable_as_float[] = $fp;
132+
133+
foreach ($not_representable_as_float as $value) {
134+
var_dump(is_representable_as_float($value));
135+
}
136+
137+
class TestClass {
138+
public function __toString() {
139+
return "42";
140+
}
141+
}
142+
143+
$obj = new TestClass();
144+
var_dump(is_representable_as_float($obj));
145+
146+
class TestClass2 {
147+
public function __toString() {
148+
return "not_numeric";
149+
}
150+
}
151+
152+
$obj2 = new TestClass2();
153+
var_dump(is_representable_as_float($obj2));
154+
155+
fclose($fp);
156+
?>
157+
--EXPECT--
158+
bool(true)
159+
bool(true)
160+
bool(true)
161+
bool(true)
162+
bool(true)
163+
bool(true)
164+
bool(true)
165+
bool(true)
166+
bool(true)
167+
bool(true)
168+
bool(true)
169+
bool(true)
170+
bool(true)
171+
bool(true)
172+
bool(true)
173+
bool(true)
174+
bool(true)
175+
bool(true)
176+
bool(true)
177+
bool(true)
178+
bool(true)
179+
bool(true)
180+
bool(true)
181+
bool(true)
182+
bool(true)
183+
bool(true)
184+
bool(true)
185+
bool(true)
186+
bool(true)
187+
bool(true)
188+
bool(true)
189+
bool(true)
190+
bool(true)
191+
bool(true)
192+
bool(true)
193+
bool(true)
194+
bool(true)
195+
bool(true)
196+
bool(true)
197+
bool(true)
198+
bool(true)
199+
bool(true)
200+
bool(true)
201+
bool(true)
202+
bool(true)
203+
bool(true)
204+
bool(true)
205+
bool(true)
206+
bool(true)
207+
bool(true)
208+
bool(true)
209+
bool(true)
210+
bool(true)
211+
bool(true)
212+
bool(true)
213+
bool(true)
214+
bool(false)
215+
bool(false)
216+
bool(false)
217+
bool(false)
218+
bool(false)
219+
bool(false)
220+
bool(false)
221+
bool(false)
222+
bool(false)
223+
bool(false)
224+
bool(false)
225+
bool(false)
226+
bool(false)
227+
bool(false)
228+
bool(false)
229+
bool(false)
230+
bool(false)
231+
bool(false)
232+
bool(false)
233+
bool(false)
234+
bool(false)
235+
bool(false)
236+
bool(false)
237+
bool(false)
238+
bool(false)
239+
bool(false)
240+
bool(false)
241+
bool(false)
242+
bool(false)
243+
bool(false)
244+
bool(true)
245+
bool(false)

0 commit comments

Comments
 (0)