Skip to content

BcMath\Number methods are not 1-to-1 with bcmath functions due to int argument signature #18674

Closed as not planned
@zimzat

Description

@zimzat

Description

The following code:

<?php
var_dump(
    (string)new BcMath\Number(5)->sub(4.2, 1),
    bcsub(5, 4.2, 1),
);

Resulted in this output:

string(3) "1.0"
string(3) "0.8"

But I expected this output instead:

string(3) "0.8"
string(3) "0.8"

The signature of bcsub only accepts string which means integers and floats will both be cast to string, while BcMath\Number::sub allows integers, causing it to truncate the float and emit a deprecation notice instead of preserving the decimal places.

 bcsub(string $num1, string $num2, ?int $scale = null): string
 public BcMath\Number::sub(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number

Ultimately this means usages of bcmath functions cannot be automatically converted to objects and developers manually typing static math operations must enclose them in strings to avoid getting erroneous results.

PHP Version

PHP 8.4.7 (cli) (built: May  9 2025 06:54:08) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.7, Copyright (c), by Zend Technologies

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions