From a521fe51d34c64b216fbeb89060eaaefbfb2fc52 Mon Sep 17 00:00:00 2001 From: Andrea Faulds Date: Tue, 15 Jul 2014 00:35:26 +0100 Subject: Implemented intdiv() --- ext/standard/basic_functions.c | 6 ++++++ ext/standard/math.c | 19 ++++++++++++++++++- ext/standard/php_math.h | 1 + ext/standard/tests/math/intdiv.phpt | 23 +++++++++++++++++++++++ ext/standard/tests/math/intdiv_64bit.phpt | 13 +++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 ext/standard/tests/math/intdiv.phpt create mode 100644 ext/standard/tests/math/intdiv_64bit.phpt (limited to 'ext/standard') 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-- + +--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-- + +--EXPECTF-- +int(3074457345618258602) \ No newline at end of file -- cgit v1.2.1