-3

According to this wiki page:

37975227936943673922808872755445627854565536638199 × 40094690950920881030683735292761468389214899724061

Result should be:

1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139

I tested:

<?php
$a = '37975227936943673922808872755445627854565536638199';
$b = '40094690950920881030683735292761468389214899724061';

echo "A: ".$a."\n";
echo "B: ".$b."\n";

$c = $a * $b; 

echo "Result:\n".number_format($c, 0, '', '')."\n";
echo "Expected: \n1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139";
?>

And I got result:

A: 37975227936943673922808872755445627854565536638199
B: 40094690950920881030683735292761468389214899724061

Result: 1522605027922533518260457400714874678658400574018662242210976145948887793656784833912073618129420288

Expected: 1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139

Why does PHP show wrong result?

Is there a way to get correct result?

Community
  • 1
  • 1
Cyborg
  • 1,437
  • 19
  • 40

2 Answers2

0

Some say PHP can't handle such large numbers. But I still don't understand the result PHP outputs seems to be large enough to me, but are NOT correct.

Is there a way to get correct results?

Yes, for PHP 1 line solution (using Python3):

echo exec('python3 -c "print('.$a.' * '.$b.')"');

Results as expected:

1522605027922533360535618378132637429718068114961380688657908494580122963258952897654000350692006139

Cyborg
  • 1,437
  • 19
  • 40
0

There are php functions to perform mathematical functions on numbers which are saved as strings:

have a look at this function

Jochen
  • 25
  • 6