As a computer engineer I am totally aware of how floating point arithmetic works on the CPU (ALU) layer. This is NOT my question. My question is how to accurately subtract two floating point values without having to write hundreds of lines of boilerplate code to do so. In a script language like PHP there MUST be a simple way to do this, right? I mean we are living in the year 2020 and still have to deal with this kind of crap on the software side? Excuse my language.
I have two floats (same values) - one is retrieved from a database and the other one is retrieved via JSON request. Printing the two values with echo results in:
$a = 1865183.4082;
$b = 1865183.408200000000000000;
Now I want to subtract them and obtain the true result (precision of 10 decimals) which is zero. I tried the following three ways:
$res = $a - $b;
// $res = 7.4505805969238E-9
$res = floatval(number_format($a, 10, ".", "")) - floatval(number_format($b, 10, ".", ""));
// $res = 7.4505805969238E-9
$res = bcsub(number_format($a, 10, ".", ""), number_format($b, 10, ".", ""), 10);
// $res = 0.0000000075
I just spent two hours on finding the solution. No success. Please help.
EDIT: Someone closed this question as if I wasn't able to look at all the other questions related to this. My problem is NOT solved and there is no solution in the linked question. Thanks.