Is there something that's larger than any numbers in PHP?

前端 未结 9 1530
谎友^
谎友^ 2021-01-19 03:12

I need to simulate a ∞ in PHP.

So that min(∞,$number) is always $number.

相关标签:
9条回答
  • 2021-01-19 03:44

    You could potentially use the PHP_INT_MAX constant (click for PHP manual docs).

    However, you may want to think about whether you really need to use it - it seems like a bit of an odd request.

    0 讨论(0)
  • 2021-01-19 03:48

    min($number,$number) is always $number (also true for max() of course).

    0 讨论(0)
  • 2021-01-19 03:49

    PHP actually has a predefined constant for "infinity": INF. This isn't true infinity, but is essentially the largest float value possible. On 64-bit systems, the largest float is roughly equal to 1.8e308, so this is considered to be equal to infinity.

    $inf = INF;
    var_dump(min($inf,PHP_INT_MAX)); // outputs int(9223372036854775807)
    var_dump(min($inf,1.79e308)); // outputs float(1.79E+308)
    var_dump(min($inf,1.799e308)); // outputs float(INF)
    var_dump(min($inf,1.8e308)); // outputs float(INF)
    var_dump($inf === 1.8e308); // outputs bool(true)
    

    Note, any number with a value larger than the maximum float value will be cast to INF. So therefore if we do, var_dump($inf === 1e50000);, this will also output true even though the maximum float is less than this.

    0 讨论(0)
  • 2021-01-19 03:55

    I suppose, assuming this is an integer, you could use PHP_INT_MAX constant.

    0 讨论(0)
  • 2021-01-19 03:58

    I suppose that, for integers, you could use PHP_INT_MAX , the following code :

    var_dump(PHP_INT_MAX);
    

    Gives this output, on my machine :

    int 2147483647
    


    But you have to be careful ; see Integer overflow (quoting) :

    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.

    And, from the Floating point numbers documentation page :

    The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

    Considering the integer overflow, and depending on your case, using this kind of value might be a better (?) solution...

    0 讨论(0)
  • 2021-01-19 03:58

    Use the constant PHP_INT_MAX.

    http://php.net/manual/en/language.types.integer.php

    0 讨论(0)
提交回复
热议问题