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.