0

During one of my college competition the problem stated to take input from a file as numbers and then simply add half of it to itself. The numbers were 18 digit long integers. Most of my php output differed from actual answer (most of the differences were powers of 2).
Note: I have also set the floating point precision to 30 places so that php does not print it as 1.3434...E+18 but rather all the 18 digits.
And heres my code for the contest.

<?php
$file = fopen("100.in","r");
$inp = [];
fgets($file);

while(!feof($file))
    $inp[] = (double)fgets($file);

fclose($file);

$i = 0;
while(isset($inp[$i])) {
    $half = $inp[$i]/2;
    $sum = $inp[$i] + $half;
    echo "$sum <br>";
    $i++; 
}
?>

I have checked the input array value with values in file and there is no error while taking or storing the values as float.

Also, after I calculated the answer in python (and stored them in a file) and tried to read the actual answers as numbers, the numbers php was printing were same as that I have calculated before and not that I was reading from file.

So can someone tell me whats wrong with my code or is it a problem with php. And also how can I correct it.

You can see the debug file here

EDIT: if you see the debug file you can clearly see that the problem is not related to large numbers as the code is printing both 302309448458254344 and 1319763928349129856 which is greater than 4 times the first value.

Chinmay Chandak
  • 455
  • 1
  • 8
  • 14
  • 2
    http://stackoverflow.com/questions/4427020/is-there-a-biginteger-class-in-php AND http://stackoverflow.com/questions/2490888/how-to-work-with-big-numbers-in-php – Jeremy Harris May 17 '17 at 21:11
  • Something about floating point: http://php.net/manual/en/language.types.float.php – Todor Simeonov May 17 '17 at 21:15
  • 1
    Possible duplicate of [Is there a BigInteger class in PHP?](http://stackoverflow.com/questions/4427020/is-there-a-biginteger-class-in-php) – Terry May 17 '17 at 21:18
  • @JeremyHarris if you see the debug file you can clearly see that the problem is not related to large numbers as its printing both 302309448458254344 and 1319763928349129856 which is apprx 4times the first value. – Chinmay Chandak May 17 '17 at 21:21
  • @TodorSimeonov all the numbers are strictly 18 digits long with no decimal places i.e. to say they are large integers – Chinmay Chandak May 17 '17 at 21:23
  • Even though they are large integers, it appears PHP is storing them in IEEE 754 64-bit binary floating point. The issue with large numbers not that they are too big to be approximated, but that they are outside the range in which all integers are exactly representable. – Patricia Shanahan May 17 '17 at 21:28
  • If I read the docs correctly, if there is integer overflow, or if a value or result doesn't fit in an integer, the value is converted to a float (usually 64 bit IEEE-754 type, it seems). – Rudy Velthuis May 18 '17 at 19:35
  • @RudyVelthuis but what about reding those numbers from a file... as i said i calculated the answers in python and stored them a file. and then read them from that file there was still the same error. – Chinmay Chandak May 18 '17 at 19:41
  • It should not matter where these numbers come from. 18 digits require a 64 bit integer. Does your system have that? If not, PHP will convert to float instead, and that is not accurate for integers larger than 52 bits. Oh, Python can handle integers of (almost) *any* size, as it will automatically promote to its multi-precision big integer type, AFAIK. But not PHP. – Rudy Velthuis May 18 '17 at 19:44
  • @RudyVelthuis yes my system is 64bit. – Chinmay Chandak May 19 '17 at 15:45
  • @ChinmayChandak: The fact that your system is 64 bit does not mean that the integer size is 64 bit. I don't know PHP well enough. Is there a way to find out the maximum integer value, or the size of an integer? – Rudy Velthuis May 19 '17 at 17:29

0 Answers0