@@ -71,7 +71,11 @@ private static function format($x, $scale, $pad)
7171 } elseif ($ scale ) {
7272 $ temp [1 ] = str_repeat ('0 ' , $ scale );
7373 }
74- return $ sign . rtrim (implode ('. ' , $ temp ), '. ' );
74+ $ result = rtrim (implode ('. ' , $ temp ), '. ' );
75+ if ($ sign == '- ' && preg_match ('#^0\.?0*$# ' , $ result )) {
76+ $ sign = '' ;
77+ }
78+ return $ sign . $ result ;
7579 }
7680
7781 /**
@@ -133,9 +137,11 @@ private static function mul($x, $y, $scale, $pad)
133137 }
134138
135139 $ z = $ x ->abs ()->multiply ($ y ->abs ());
136- $ sign = (self ::isNegative ($ x ) ^ self ::isNegative ($ y )) ? '- ' : '' ;
140+ $ result = self ::format ($ z , $ scale , 2 * $ pad );
141+
142+ $ sign = (self ::isNegative ($ x ) ^ self ::isNegative ($ y )) && !preg_match ('#^0\.?0*$# ' , $ result ) ? '- ' : '' ;
137143
138- return $ sign . self :: format ( $ z , $ scale , 2 * $ pad ) ;
144+ return $ sign . $ result ;
139145 }
140146
141147 /**
@@ -402,19 +408,25 @@ public static function __callStatic($name, $arguments)
402408 }
403409 }
404410 foreach ($ numbers as $ i => $ arg ) {
411+ $ num = $ i + 1 ;
405412 switch (true ) {
406413 case is_bool ($ arg ):
407414 case is_numeric ($ arg ):
408415 case is_string ($ arg ):
409416 case is_object ($ arg ) && method_exists ($ arg , '__toString ' ):
410- case is_null ($ arg ):
411- if (!is_bool ($ arg ) && !is_null ($ arg ) && !is_numeric ("$ arg " )) {
412- trigger_error ("bc $ name: bcmath function argument is not well-formed " , E_USER_WARNING );
417+ if (!is_bool ($ arg ) && !is_numeric ("$ arg " )) {
418+ throw new \ValueError ("bc $ name: bcmath function argument is not well-formed " );
413419 }
414420 break ;
421+ // PHP >= 8.1 has deprecated the passing of nulls to string parameters
422+ case is_null ($ arg ):
423+ $ error = "bc $ name(): Passing null to parameter # $ num ( \$$ names [$ i ]) of type string is deprecated " ;
424+ trigger_error ($ error , E_USER_DEPRECATED );
425+ break ;
415426 default :
416427 $ type = is_object ($ arg ) ? get_class ($ arg ) : gettype ($ arg );
417- throw new \TypeError ("bc $ name(): Argument # $ i ( \$$ names [$ i ]) must be of type string, $ type given " );
428+ $ error = "bc $ name(): Argument # $ num ( \$$ names [$ i ]) must be of type string, $ type given " ;
429+ throw new \TypeError ($ error );
418430 }
419431 }
420432 if (!isset (self ::$ scale )) {
0 commit comments