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

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

I need to simulate a ∞ in PHP.

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

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题