Skip to content

[RFC] Add is_representable_as_float() and is_representable_as_int #19308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -3700,6 +3700,16 @@ function is_iterable(mixed $value): bool {}
*/
function is_countable(mixed $value): bool {}

/**
* @compile-time-eval
*/
function is_representable_as_float(mixed $value): bool {}

/**
* @compile-time-eval
*/
function is_representable_as_int(mixed $value): bool {}

/* uniqid.c */

#ifdef HAVE_GETTIMEOFDAY
Expand Down
10 changes: 9 additions & 1 deletion ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

245 changes: 245 additions & 0 deletions ext/standard/tests/general_functions/is_representable_as_float.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
--TEST--
Test is_representable_as_float() function
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64-bit systems only"); ?>
--FILE--
<?php
$representable_as_float = array(
0,
1,
-1,
42,
-42,

9007199254740992, // 2^53
-9007199254740992, // -2^53
9007199254740991, // 2^53 - 1
-9007199254740991, // -(2^53 - 1)

// from 2^53 to 2^54, only even numbers are exactly representable
9007199254740994, // 2^53 + 2
9007199254740996, // 2^53 + 4
18014398509481984, // 2^54
-9007199254740994, // -(2^53 + 2)

// from 2^54 to 2^55, only multiples of 4 are exactly representable
18014398509481988, // 2^54 + 4
18014398509481992, // 2^54 + 8
36028797018963968, // 2^55

// from 2^55 to 2^56, only multiples of 8 are exactly representable
36028797018963976, // 2^55 + 8
36028797018963984, // 2^55 + 16
72057594037927936, // 2^56

// pure powers of two
1152921504606846976, // 2^60
2305843009213693952, // 2^61

PHP_INT_MIN,

0.0,
-0.0,
1.0,
-1.0,
3.14,
-3.14,

INF,
-INF,
NAN,

2,
4,
8,
16,
1024,

"0",
"1",
"-1",
"42",
"0.0",
"1.0",
"-1.0",
"3.14",
"-3.14",
"1e10",
"-1e10",
"1.5e-10",
"9007199254740992", // 2^53
"-9007199254740992", // -2^53


"9007199254740994", // 2^53 + 2
"18014398509481988", // 2^54 + 4
"36028797018963976", // 2^55 + 8

" 42 ",
"\t1.5\n",
"\r-3.14\v",
);

foreach ($representable_as_float as $value) {
var_dump(is_representable_as_float($value));
}

$not_representable_as_float = array(
9007199254740993, // 2^53 + 1
-9007199254740993, // -(2^53 + 1)
9007199254740995, // 2^53 + 3

// odd numbers in 2^53 to 2^54 range
9007199254740995, // 2^53 + 3
9007199254740997, // 2^53 + 5
18014398509481985, // 2^54 + 1

// not multiples of 4 in 2^54 to 2^55 range
18014398509481986, // 2^54 + 2
18014398509481990, // 2^54 + 6
36028797018963970, // 2^55 + 2

// not multiples of 8 in 2^55 to 2^56 range
36028797018963972, // 2^55 + 4
36028797018963978, // 2^55 + 10
72057594037927940, // 2^56 + 4
null,
true,
false,
"",
"hello",
"123.456.789",
"1e400",
"-1e400",

[],
[1, 2, 3],
new stdClass(),


"9007199254740993", // 2^53 + 1
"18014398509481986", // 2^54 + 2 (not multiple of 4)
"36028797018963972", // 2^55 + 4 (not multiple of 8)

"0x42",
"0xFF",

"0b1010",
);

$fp = fopen(__FILE__, "r");
$not_representable_as_float[] = $fp;

foreach ($not_representable_as_float as $value) {
var_dump(is_representable_as_float($value));
}

class TestClass {
public function __toString() {
return "42";
}
}

$obj = new TestClass();
var_dump(is_representable_as_float($obj));

class TestClass2 {
public function __toString() {
return "not_numeric";
}
}

$obj2 = new TestClass2();
var_dump(is_representable_as_float($obj2));

fclose($fp);
?>
--EXPECT--
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
Loading