Hey, first post here, hopefully you guys can help.
I have some legacy C++ code I need to convert to PHP, I have a running version of the C++ routine, as well as the code for that routine.
Here's the C++ line:
(I have simplified it a bit to display the problem)
int n_1,n_2,n_3,n_4;
n_1 = 947;
n_2 = 1842;
n_4 = 7609;
n_3 = ((((n_1 * n_2) * (n_4 + 6)) + n_4 + 29) / (n_1)) * n_1;
while (n_3 > 9999)
n_3 -= 8888;
while (n_3 < 1000)
n_3 += 1237;
In C++ the result is n_3 = 2443;
As you can see there are several issues here, the most critical is that the calculation of n_3 "overflows" because of the int maximum values.
The other "debugging" problem is that the "while's" prevent me from seeing what the actual calculation returned.
When I run this in PHP:
$n_1 = 947;
$n_2 = 1842;
$n_4 = 7609;
$n_3 = (((($n_1 * $n_2) * ($n_4 + 6)) + $n_4 + 29) / ($n_1)) * $n_1;
while ($n_3 > 9999)
$n_3 -= 8888;
while ($n_3 < 1000)
$n_3 += 1237;
$n_3 = 6344;
As far as I can tell, the PHP code is actually correct, and the C++ code is buggy because of the "misuse" of the int type, however I need to replicate this "bug" in PHP so that I can get the exact same value of $n_3 as C++ would give me.
Any help would be most appreciated, I have played with decbin and bindec to remove bits from the number after and during the calculation, however I think I'm probably missing a fundamental understanding of how int works in C++
Thanks again!