diff options
author | Andrea Faulds <ajf@ajf.me> | 2014-07-15 00:35:26 +0100 |
---|---|---|
committer | Andrea Faulds <ajf@ajf.me> | 2014-07-15 00:35:26 +0100 |
commit | a521fe51d34c64b216fbeb89060eaaefbfb2fc52 (patch) | |
tree | 6445a850caf2143e72ad55779aa06d4c7bf3bf46 /ext/standard | |
parent | 1fef4e87fcba82e4e937382063483ee61f764936 (diff) | |
download | php-git-a521fe51d34c64b216fbeb89060eaaefbfb2fc52.tar.gz |
Implemented intdiv()
Diffstat (limited to 'ext/standard')
-rw-r--r-- | ext/standard/basic_functions.c | 6 | ||||
-rw-r--r-- | ext/standard/math.c | 19 | ||||
-rw-r--r-- | ext/standard/php_math.h | 1 | ||||
-rw-r--r-- | ext/standard/tests/math/intdiv.phpt | 23 | ||||
-rw-r--r-- | ext/standard/tests/math/intdiv_64bit.phpt | 13 |
5 files changed, 61 insertions, 1 deletions
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 5ca7ec0ffe..b6915df3dc 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1758,6 +1758,11 @@ ZEND_BEGIN_ARG_INFO(arginfo_fmod, 0) ZEND_ARG_INFO(0, x) ZEND_ARG_INFO(0, y) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_intdiv, 0) + ZEND_ARG_INFO(0, numerator) + ZEND_ARG_INFO(0, divisor) +ZEND_END_ARG_INFO() /* }}} */ /* {{{ md5.c */ ZEND_BEGIN_ARG_INFO_EX(arginfo_md5, 0, 0, 1) @@ -2894,6 +2899,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(base_convert, arginfo_base_convert) PHP_FE(number_format, arginfo_number_format) PHP_FE(fmod, arginfo_fmod) + PHP_FE(intdiv, arginfo_intdiv) #ifdef HAVE_INET_NTOP PHP_RAW_NAMED_FE(inet_ntop, php_inet_ntop, arginfo_inet_ntop) #endif diff --git a/ext/standard/math.c b/ext/standard/math.c index 65702ddf14..281786a81a 100644 --- a/ext/standard/math.c +++ b/ext/standard/math.c @@ -1247,7 +1247,24 @@ PHP_FUNCTION(fmod) } /* }}} */ - +/* {{{ proto int intdiv(int numerator, int divisor) + Returns the integer division of the numerator by the divisor */ +PHP_FUNCTION(intdiv) +{ + long numerator, divisor; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &numerator, &divisor) == FAILURE) { + return; + } + + if (divisor == 0) { + php_error(E_WARNING, "Division by zero"); + RETURN_BOOL(0); + } + + RETURN_LONG(numerator/divisor); +} +/* }}} */ /* * Local variables: diff --git a/ext/standard/php_math.h b/ext/standard/php_math.h index 8dec21acf3..8c168e7d3a 100644 --- a/ext/standard/php_math.h +++ b/ext/standard/php_math.h @@ -66,6 +66,7 @@ PHP_FUNCTION(number_format); PHP_FUNCTION(fmod); PHP_FUNCTION(deg2rad); PHP_FUNCTION(rad2deg); +PHP_FUNCTION(intdiv); /* WARNING: these functions are expermental: they could change their names or diff --git a/ext/standard/tests/math/intdiv.phpt b/ext/standard/tests/math/intdiv.phpt new file mode 100644 index 0000000000..9dbbfb8ad6 --- /dev/null +++ b/ext/standard/tests/math/intdiv.phpt @@ -0,0 +1,23 @@ +--TEST-- +intdiv functionality +--FILE-- +<?php +var_dump(intdiv(3, 2)); +var_dump(intdiv(-3, 2)); +var_dump(intdiv(3, -2)); +var_dump(intdiv(-3, 2)); +var_dump(intdiv(PHP_INT_MAX, PHP_INT_MAX)); +var_dump(intdiv(-PHP_INT_MAX - 1, -PHP_INT_MAX - 1)); +var_dump(intdiv(1, 0)); + +?> +--EXPECTF-- +int(1) +int(-1) +int(-1) +int(-1) +int(1) +int(1) + +Warning: Division by zero in %s on line 8 +bool(false) diff --git a/ext/standard/tests/math/intdiv_64bit.phpt b/ext/standard/tests/math/intdiv_64bit.phpt new file mode 100644 index 0000000000..320e480291 --- /dev/null +++ b/ext/standard/tests/math/intdiv_64bit.phpt @@ -0,0 +1,13 @@ +--TEST-- +intdiv functionality +--SKIPIF-- +if (PHP_INT_SIZE !== 8) { + die("skip this test is for 64-bit platforms only"); +} +--FILE-- +<?php +// (int)(PHP_INT_MAX / 3) gives a different result +var_dump(intdiv(PHP_INT_MAX, 3)); +?> +--EXPECTF-- +int(3074457345618258602)
\ No newline at end of file |