rfc:allow_int_args_to_bcmath_function

PHP RFC: Allow int type argument to BCMath function

Introduction

Currently, BCMath functions only accept values ​​as strings. If want to calculate an int type with BCMath, need to convert it to a string once, which incurs unnecessary conversion costs.

Proposal

This RFC proposes adding an int type to the BCMath function argument and making the argument a union type of int and string.

That is, the function signature changes to:

function bcadd(int|string $num1, int|string $num2, ?int $scale = null): string {}
 
function bcsub(int|string $num1, int|string $num2, ?int $scale = null): string {}
 
function bcmul(int|string $num1, int|string $num2, ?int $scale = null): string {}
 
function bcdiv(int|string $num1, int|string $num2, ?int $scale = null): string {}
 
function bcmod(int|string $num1, int|string $num2, ?int $scale = null): string {}
 
function bcpowmod(int|string $num, int|string $exponent, int|string $modulus, ?int $scale = null): string {}
 
function bcpow(int|string $num, int|string $exponent, ?int $scale = null): string {}
 
function bcsqrt(int|string $num, ?int $scale = null): string {}
 
function bccomp(int|string $num1, int|string $num2, ?int $scale = null): int {}
 
function bcround(int|string $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): string {}

Note that even if the only numbers passed as arguments are ints, the return value will always be a string.

I do not change anything for the following three functions. bcscale() is not a calculation function, so exclude it. bcfloor() and bcceil() are excluded because there is no point in allowing int type arguments due to the characteristics of their functions.

function bcscale(?int $scale = null): int {}
 
function bcfloor(string $num): string {}
 
function bcceil(string $num): string {}

Backward Incompatible Changes

Any code that uses strict types and expects an error to occur when passing an int to these functions is affected because the error no longer occurs.

Proposed PHP Version(s)

next PHP 8.x (8.4)

RFC Impact

To SAPIs

None.

To Existing Extensions

Only BCMath.

To Opcache

None.

New Constants

None.

php.ini Defaults

None.

Open Issues

None.

Unaffected PHP Functionality

There is no effect on anything other than BCMath.

Future Scope

None.

Proposed Voting Choices

There is a yes/no choice whether to accept this RFC and requires a 2/3 majority vote to be accepted.

Patches and Tests

yet

Implementation

yet

References

Rejected Features

None.

rfc/allow_int_args_to_bcmath_function.txt · Last modified: 2024/07/08 01:20 by saki