How to get the highest numerical value supported by php?

Asked

Viewed 304 times

2

Is there any php method to get the highest number value supported by it?

  • if one of the answers solved the problem mark it as correct please. Read: https://pt.meta.stackoverflow.com/a/1079/3635

3 answers

6

PHP has the predefined constant of PHP_INT_MAX, since 5.0.5, and also the PHP_INT_MIN, since PHP 7.0. The first will return the highest possible value and the second the lowest possible value.

<?php

echo 'Máximo: '  .PHP_INT_MAX;
echo PHP_EOL;
echo 'Minimo: ' . PHP_INT_MIN;

Test it out here.

Normally, on 32-bit systems will return 2147483647 and -2147483648, respectively. While in 64 bits will return 9223372036854775807 and -9223372036854775808.

5

With the constant PHP_INT_MAX, which is the largest integer supported in that interpreter, which is available from the version 5.0.5.

Online Example

Reference: Predefined Constants

3

Whole

Official documentation:

The size of an integer is dependent on the platform, although a maximum value of about two billion is the usual value (that is, the largest flagged value represented with 32 bits). 64-bit platforms have a maximum value close to 9e18, except in Windows, versions prior to PHP 7, which will always be 32 bits. PHP does not support unmarked integers. The whole size can be determined by the constant PHP_INT_SIZE and the maximum value through the constant PHP_INT_MAX, since PHP version 5.0.5. From PHP version 7.0.0, the minimum value can be obtained through the constant PHP_INT_MIN.

Floating Point

Official documentation:

The size of a floating point number depends on the platform, the maximum being ~1.8e308 with 14 decimal digit accuracy (64-bit representation in format IEEE 754).

Warning: Floating point numbers have limited accuracy. Although it relies on the system, PHP generally uses the IEEE 754 double-precision format, which will bring maximum accuracy due to rounding of the order of 1.11e-16. Unusual mathematical operations may cause larger errors, and of course the propagation of errors should be considered when several operations are performed.

In addition, rational numbers that have exact representation in numbers in base 10, such as 0.1 or 0.7, do not have exact representation in floating point in base 2, the format used internally, no matter the size of the mantissa. So there is no conversion to the internal format without a small loss of accuracy. This can lead to confusing results: for example, floor((0.1+0.7)*10) will normally return 7, instead of the expected result 8, because the final internal representation will be something like 7.9999999991118....

So never rely on results with floating point numbers up to the last house, and never compare floating point numbers on equals. If you really need high precision, you can use the mathematical functions of arbitrary precision and the functions gmp are available.

For a "simple" explanation of this question, see guide on floating point, which also has the alternate title "Because my numbers don’t add up right?".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.