Skip to content

Commit 8f20b99

Browse files
committed
Expect number argument in round()
1 parent 1d631c1 commit 8f20b99

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

ext/standard/math.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ static double php_expm1(double x)
281281
}
282282
/* }}}*/
283283

284-
/* {{{ proto int|float abs(int number)
284+
/* {{{ proto int|float abs(int|float number)
285285
Return the absolute value of the number */
286286
PHP_FUNCTION(abs)
287287
{
@@ -345,7 +345,7 @@ PHP_FUNCTION(floor)
345345
}
346346
/* }}} */
347347

348-
/* {{{ proto float|false round(float number [, int precision [, int mode]])
348+
/* {{{ proto float round(float number [, int precision [, int mode]])
349349
Returns the number rounded to specified precision */
350350
PHP_FUNCTION(round)
351351
{
@@ -356,7 +356,7 @@ PHP_FUNCTION(round)
356356
double return_val;
357357

358358
ZEND_PARSE_PARAMETERS_START(1, 3)
359-
Z_PARAM_ZVAL(value)
359+
Z_PARAM_NUMBER(value)
360360
Z_PARAM_OPTIONAL
361361
Z_PARAM_LONG(precision)
362362
Z_PARAM_LONG(mode)
@@ -373,7 +373,6 @@ PHP_FUNCTION(round)
373373
places = precision;
374374
#endif
375375
}
376-
convert_scalar_to_number_ex(value);
377376

378377
switch (Z_TYPE_P(value)) {
379378
case IS_LONG:
@@ -389,9 +388,7 @@ PHP_FUNCTION(round)
389388
RETURN_DOUBLE(return_val);
390389
break;
391390

392-
default:
393-
RETURN_FALSE;
394-
break;
391+
EMPTY_SWITCH_DEFAULT_CASE()
395392
}
396393
}
397394
/* }}} */

ext/standard/tests/math/round_variation1.phpt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ $inputs = array(
8181
// loop through each element of $inputs to check the behaviour of round()
8282
$iterator = 1;
8383
foreach($inputs as $input) {
84-
echo "\n-- Iteration $iterator --\n";
85-
var_dump(round($input, 14));
86-
$iterator++;
84+
echo "\n-- Iteration $iterator --\n";
85+
try {
86+
var_dump(round($input, 14));
87+
} catch (TypeError $e) {
88+
echo $e->getMessage(), "\n";
89+
}
90+
$iterator++;
8791
};
8892
fclose($fp);
8993
?>
9094
===Done===
91-
--EXPECTF--
95+
--EXPECT--
9296
*** Testing round() : usage variations ***
9397

9498
-- Iteration 1 --
@@ -140,27 +144,25 @@ float(1)
140144
float(0)
141145

142146
-- Iteration 17 --
143-
float(0)
147+
round() expects parameter 1 to be int or float, string given
144148

145149
-- Iteration 18 --
146-
float(0)
150+
round() expects parameter 1 to be int or float, string given
147151

148152
-- Iteration 19 --
149-
bool(false)
153+
round() expects parameter 1 to be int or float, array given
150154

151155
-- Iteration 20 --
152-
float(0)
156+
round() expects parameter 1 to be int or float, string given
153157

154158
-- Iteration 21 --
155-
float(0)
159+
round() expects parameter 1 to be int or float, string given
156160

157161
-- Iteration 22 --
158-
float(0)
162+
round() expects parameter 1 to be int or float, string given
159163

160164
-- Iteration 23 --
161-
162-
Notice: Object of class classA could not be converted to number in %s on line %d
163-
float(1)
165+
round() expects parameter 1 to be int or float, object given
164166

165167
-- Iteration 24 --
166168
float(0)
@@ -169,5 +171,5 @@ float(0)
169171
float(0)
170172

171173
-- Iteration 26 --
172-
float(%f)
174+
round() expects parameter 1 to be int or float, resource given
173175
===Done===

0 commit comments

Comments
 (0)