Doc #76287 [Com]: bcmod returns incorrect values
| From: | kacper dot rowinski at gmail dot com | Date: | Mon, 30 Apr 2018 17:20:43 +0000 |
| Subject: | Doc #76287 [Com]: bcmod returns incorrect values | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-15636@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76287&edit=1
ID: 76287
Comment by: kacper dot rowinski at gmail dot com
Reported by: kacper dot rowinski at gmail dot com
Summary: bcmod returns incorrect values
Status: Analyzed
Type: Documentation Problem
Package: BC math related
Operating System: Linux debian
PHP Version: 7.2.5
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Tx for info I hope this will be soon added to documentation as it's little confusing.
Previous Comments:
------------------------------------------------------------------------
[2018-04-30 13:54:06] cmb@php.net
bcmod() doesn't use floor() but rather truncates towards zero,
which is also defined this way for POSIX fmod(), so that the
result always has the same sign as the dividend. Therefore, this
is not a bug, but rather a documentation issue.
------------------------------------------------------------------------
[2018-04-30 12:36:40] kacper dot rowinski at gmail dot com
Prove https://www.wolframalpha.com/input/?i=0.00000000099999+mod+-0.00056
------------------------------------------------------------------------
[2018-04-30 12:33:45] kacper dot rowinski at gmail dot com
Description:
------------
Using bcmod on float return incorrect value
Test script:
---------------
<?php
$leftOperand = '0.00000000099999';
$modulus = '-0.00056';
$scale =14;
// incorrect :
echo bcmod($leftOperand,$modulus, $scale);
// this how it should work
// mod(a, b) = a - b * floor(a/b)
/*
echo bcsub(
$leftOperand,
bcmul(
$modulus,
floor(bcdiv($leftOperand, $modulus, $scale)),
$scale
),
$scale
);
*/
Expected result:
----------------
-0.00055999900001
Actual result:
--------------
0.00000000099999
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76287&edit=1