PHP interprets numbers as floats even when they're less than INT_MAX












0















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










share|improve this question

























  • 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
















0















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










share|improve this question

























  • 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














0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 22:26







FatalError

















asked Nov 25 '18 at 21:36









FatalErrorFatalError

4131311




4131311













  • 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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.






    share|improve this answer




























      3














      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.






      share|improve this answer


























        3












        3








        3







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 22:28









        johannesjohannes

        13k13355




        13k13355






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks