The following code:
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1) + "<br />");
echo sprintf('%0.0f', (bcsub($val, 1)) + "<br />");
In windows 32 bit outputs:
209810563658809344
209810563658809344
How can be made the subtraction to work properly?
Both your sprint() masking and the use of +
instead of .
for concatenation are forcing a cast to numeric datatype
$val = "209810563658809344";
echo sprintf('%0.0f', ($val - 1)) . '<br />';
echo bcsub($val, 1) . '<br />';
Avoid the conversion to floating point and you'll be fine.
$val = "20981056365880934";
echo bcmul($val, 2, 0)." ≈ ".bcdiv($val, 2, 0)." × 2 + ".bcsub($val, 1, 0)." + 1";
Note the third scale parameter which allows you to control precision like printf()
. See the bc manual