Skip to content

Fixed GH-17275: Fixed the calculation logic of dividend scale. #17279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions ext/bcmath/libbcmath/src/div.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,24 +427,30 @@ bool bc_divide(bc_num numerator, bc_num divisor, bc_num *quot, size_t scale)
return true;
}

/* Length of numerator data that can be read */
size_t numerator_readable_len = numeratorend - numeratorptr + 1;

/* set scale to numerator */
if (numerator_scale > scale) {
size_t scale_diff = numerator_scale - scale;
if (numerator_bottom_extension > scale_diff) {
numerator_bottom_extension -= scale_diff;
} else {
numerator_bottom_extension = 0;
numeratorend -= scale_diff > numerator_top_extension ? scale_diff - numerator_top_extension : 0;
if (EXPECTED(numerator_readable_len > scale_diff)) {
numerator_readable_len -= scale_diff;
numeratorend -= scale_diff;
} else {
numerator_readable_len = 0;
numeratorend = numeratorptr;
}
}
numerator_top_extension = MIN(numerator_top_extension, scale);
} else {
numerator_bottom_extension += scale - numerator_scale;
}
numerator_scale = scale;

/* Length of numerator data that can be read */
size_t numerator_readable_len = numeratorend - numeratorptr + 1;

if (divisor_len > numerator_readable_len + numerator_bottom_extension) {
*quot = bc_copy_num(BCG(_zero_));
return true;
Expand Down
14 changes: 14 additions & 0 deletions ext/bcmath/tests/gh17275.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
GH-17275 Incorrect result of bcdiv function
--EXTENSIONS--
bcmath
--FILE--
<?php
var_dump(
bcdiv('0.03772321', '9650.0', 8),
bcdiv('0.03772321', '9650.0', 9),
);
?>
--EXPECT--
string(10) "0.00000390"
string(11) "0.000003909"
Loading