PHP interprets numbers as floats even when they're less than INT_MAX
PHP documentation here states that:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
But what about an operation which results in a number less than PHP_INT_MAX ?
See this code snippet as an example:
$max_int = 2**31-1 ; // 2147483647
var_dump(PHP_INT_MAX === $max_int); // false
As you can see, even when an operation results in a valid int value PHP seems to cast the result into float
var_dump(PHP_INT_MAX === (int) $max_int) // true
My questions:
Does PHP interpreter cast the result into float before making any calculations?
Shouldn't PHP calculate the result and then sets the type accordingly? (Makes sense right?)
Edit:
PHP version: 7.2.1 32-bit
OS: Windows: 10 x64
I'm using XAMPP
php integer-overflow
add a comment |
PHP documentation here states that:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
But what about an operation which results in a number less than PHP_INT_MAX ?
See this code snippet as an example:
$max_int = 2**31-1 ; // 2147483647
var_dump(PHP_INT_MAX === $max_int); // false
As you can see, even when an operation results in a valid int value PHP seems to cast the result into float
var_dump(PHP_INT_MAX === (int) $max_int) // true
My questions:
Does PHP interpreter cast the result into float before making any calculations?
Shouldn't PHP calculate the result and then sets the type accordingly? (Makes sense right?)
Edit:
PHP version: 7.2.1 32-bit
OS: Windows: 10 x64
I'm using XAMPP
php integer-overflow
Do you know is the value ofPHP_INT_MAX
?
– Pablo
Nov 25 '18 at 22:05
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
Please see the edit
– FatalError
Nov 25 '18 at 22:27
add a comment |
PHP documentation here states that:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
But what about an operation which results in a number less than PHP_INT_MAX ?
See this code snippet as an example:
$max_int = 2**31-1 ; // 2147483647
var_dump(PHP_INT_MAX === $max_int); // false
As you can see, even when an operation results in a valid int value PHP seems to cast the result into float
var_dump(PHP_INT_MAX === (int) $max_int) // true
My questions:
Does PHP interpreter cast the result into float before making any calculations?
Shouldn't PHP calculate the result and then sets the type accordingly? (Makes sense right?)
Edit:
PHP version: 7.2.1 32-bit
OS: Windows: 10 x64
I'm using XAMPP
php integer-overflow
PHP documentation here states that:
If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.
But what about an operation which results in a number less than PHP_INT_MAX ?
See this code snippet as an example:
$max_int = 2**31-1 ; // 2147483647
var_dump(PHP_INT_MAX === $max_int); // false
As you can see, even when an operation results in a valid int value PHP seems to cast the result into float
var_dump(PHP_INT_MAX === (int) $max_int) // true
My questions:
Does PHP interpreter cast the result into float before making any calculations?
Shouldn't PHP calculate the result and then sets the type accordingly? (Makes sense right?)
Edit:
PHP version: 7.2.1 32-bit
OS: Windows: 10 x64
I'm using XAMPP
php integer-overflow
php integer-overflow
edited Nov 25 '18 at 22:26
FatalError
asked Nov 25 '18 at 21:36
FatalErrorFatalError
4131311
4131311
Do you know is the value ofPHP_INT_MAX
?
– Pablo
Nov 25 '18 at 22:05
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
Please see the edit
– FatalError
Nov 25 '18 at 22:27
add a comment |
Do you know is the value ofPHP_INT_MAX
?
– Pablo
Nov 25 '18 at 22:05
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
Please see the edit
– FatalError
Nov 25 '18 at 22:27
Do you know is the value of
PHP_INT_MAX
?– Pablo
Nov 25 '18 at 22:05
Do you know is the value of
PHP_INT_MAX
?– Pablo
Nov 25 '18 at 22:05
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
Please see the edit
– FatalError
Nov 25 '18 at 22:27
Please see the edit
– FatalError
Nov 25 '18 at 22:27
add a comment |
1 Answer
1
active
oldest
votes
When calculating $max_int = 2**31-1
the engine does this in steps:
$tmp = 2**31;
$max_int = $tmp-1
Here $tmp
is bigger than maximum integer value and converted to a floatng point number. In consequence there is an float subtraction, resulting in a float. Since it had been float it has to stay float.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472253%2fphp-interprets-numbers-as-floats-even-when-theyre-less-than-int-max%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When calculating $max_int = 2**31-1
the engine does this in steps:
$tmp = 2**31;
$max_int = $tmp-1
Here $tmp
is bigger than maximum integer value and converted to a floatng point number. In consequence there is an float subtraction, resulting in a float. Since it had been float it has to stay float.
add a comment |
When calculating $max_int = 2**31-1
the engine does this in steps:
$tmp = 2**31;
$max_int = $tmp-1
Here $tmp
is bigger than maximum integer value and converted to a floatng point number. In consequence there is an float subtraction, resulting in a float. Since it had been float it has to stay float.
add a comment |
When calculating $max_int = 2**31-1
the engine does this in steps:
$tmp = 2**31;
$max_int = $tmp-1
Here $tmp
is bigger than maximum integer value and converted to a floatng point number. In consequence there is an float subtraction, resulting in a float. Since it had been float it has to stay float.
When calculating $max_int = 2**31-1
the engine does this in steps:
$tmp = 2**31;
$max_int = $tmp-1
Here $tmp
is bigger than maximum integer value and converted to a floatng point number. In consequence there is an float subtraction, resulting in a float. Since it had been float it has to stay float.
answered Nov 25 '18 at 22:28
johannesjohannes
13k13355
13k13355
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472253%2fphp-interprets-numbers-as-floats-even-when-theyre-less-than-int-max%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you know is the value of
PHP_INT_MAX
?– Pablo
Nov 25 '18 at 22:05
@FatalError, I'm not sure that it happens like that for lesser integers. Please have a look here: 3v4l.org/S9hmd Could you also provide more info about the environment (os, PHP version) where this was happening?
– Dan D.
Nov 25 '18 at 22:15
Please see the edit
– FatalError
Nov 25 '18 at 22:27