2

I have 2 big numbers 1133853003571025.63 and 1732254953579959.21 the sum of these numbers must be 2866107957150984.84
I'm working with php 5.6 and set the precision parameter to 16, but still php displays an incorrect output.

The output I'm getting is:
number 1: 1133853003571026
number 2: 1732254953579959
Sum : 2866107957150985
number 1 (format): 1133853003571025.75
number 2 (format): 1732254953579959.25
Sum (format): 2866107957150985.00
number length: 16

My code is

<?
$num01=1133853003571025.63;
$num02=1732254953579959.21;
$longitud= strlen($num01);
echo "number 1: ".$num01."<br>";
echo "number 2: ".$num02."<br>";
echo "Sum : ".($num01+$num02)."<br>";
echo "number 1 (format): ".number_format($num01, 2, '.', '')."<br>";
echo "number 2 (format): ".number_format($num02, 2, '.', '')."<br>";
echo "Sum (format): ".number_format(($num01+$num02), 2, '.', '')."<br>";
echo "<b>number length: </b>".$longitud."<br>";
?>

My question is what is wrong?, what is missing in my code?
Emilio Galarraga
  • 659
  • 1
  • 8
  • 14

1 Answers1

1

PHP silently converts integer numbers that are too large for PHP to handle (> PHP_INT_MAX) to floats. This is by design.

Floating point values in PHP have limited precision. Take PHP_FLOAT_EPSILON into account when doing calculations with floats.

If all this does not suit your needs, take a look at the bcmath extention for arbitrairy precision. You could use it like this:

$a = '1133853003571025.63';
$b = '1732254953579959.21';
print bcadd($a, $b, 2);  // output: '2866107957150984.84'

An alternative to using bcmath is to let a database do the calculations, you could pass the calculation in an SQL SELECT statement and retrieve the results. Have a look at PDO.

MySQL / MariaDB example query:

SELECT CAST(1133853003571025.63 + 1732254953579959.21 AS CHAR) AS bignumber;
  -- result: '2866107957150984.84'

PostgreSQL example query:

SELECT (1133853003571025.63 + 1732254953579959.21)::text AS bignumber;
  -- result: '2866107957150984.84'
Code4R7
  • 2,600
  • 1
  • 19
  • 42
  • Thanks for your answer. I tried storing the numbers in a database and adding them later. But the numbers are stored incorrectly even using the double precision data type. Searching the web, I found this page that does the calculation correctly, https: //keisan.casio.com/calculator. I just don't know how it is made and which programming language it works with. – Emilio Galarraga Jun 27 '21 at 16:17
  • I've updated the answer with a code example for [bcadd](https://www.php.net/manual/en/function.bcadd.php) that returns the output that you need. – Code4R7 Jun 27 '21 at 20:02
  • Thanks for your help, The php script is fine. The sql statement works fine with mysql but I use postgresql. `postgres=# SELECT CAST(1133853003571025.63 + 1732254953579959.21 AS CHAR) AS bignumber; bignumber ----------- 2 (1 row) ` – Emilio Galarraga Jun 27 '21 at 23:25
  • `SELECT CAST(1133853003571025.63 + 1732254953579959.21 AS CHAR(19)) AS bignumber;` it works. – Emilio Galarraga Jun 28 '21 at 22:52
  • Glad it worked. While PostgreSQL has become my bias after using MySQL for quite a while, many people are still using MySQL / MariaDB. Thanks for adding a PostgreSQL example. I'll add it to the answer. – Code4R7 Jun 29 '21 at 10:11