com php-src: Implemented intdiv(): ext/standard/basic_functio ns.c ext/standard/math.c ext/standard/php_math.h ext/standard/tests/math/intdiv.phpt
ext/standard/tests/math /intdiv_64bit.phpt
| From: | ajf@php.net | Date: | Mon, 14 Jul 2014 23:35:26 +0000 |
| Subject: | com php-src: Implemented intdiv(): ext/standard/basic_functio ns.c ext/standard/math.c ext/standard/php_math.h ext/standard/tests/math/intdiv.phpt ext/standard/tests/math /intdiv_64bit.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-79760@lists.php.net to get a copy of this message | ||
Commit: a521fe51d34c64b216fbeb89060eaaefbfb2fc52
Author: Andrea Faulds <ajf@ajf.me> Tue, 15 Jul 2014 00:35:26 +0100
Parents: 1fef4e87fcba82e4e937382063483ee61f764936
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=a521fe51d34c64b216fbeb89060eaaefbfb2fc52
Log:
Implemented intdiv()
Changed paths:
M ext/standard/basic_functions.c
M ext/standard/math.c
M ext/standard/php_math.h
A ext/standard/tests/math/intdiv.phpt
A ext/standard/tests/math/intdiv_64bit.phpt
Diff:
diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 5ca7ec0..b6915df 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 65702dd..281786a 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 8dec21a..8c168e7 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 0000000..9dbbfb8
--- /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 0000000..320e480
--- /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