Skip to content

Add fdiv() function #4769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,11 @@ ZEND_BEGIN_ARG_INFO(arginfo_fmod, 0)
ZEND_ARG_INFO(0, y)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_fdiv, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it could add Reflection param and return types, with/without being generated by stubs (some of the above functions already do)

$dividend and $divisor could be used as names to match intdiv.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect that these functions will be converted to stubs very soon, so I'm leaving it for now.

ZEND_ARG_INFO(0, dividend)
ZEND_ARG_INFO(0, divisor)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_intdiv, 0)
ZEND_ARG_INFO(0, dividend)
ZEND_ARG_INFO(0, divisor)
Expand Down Expand Up @@ -2287,6 +2292,7 @@ static 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(fdiv, arginfo_fdiv)
PHP_FE(intdiv, arginfo_intdiv)
#ifdef HAVE_INET_NTOP
PHP_RAW_NAMED_FE(inet_ntop, zif_inet_ntop, arginfo_inet_ntop)
Expand Down
19 changes: 19 additions & 0 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,25 @@ PHP_FUNCTION(fmod)
}
/* }}} */

/* {{{ proto float fdiv(float dividend, float divisor)
Perform floating-point division of dividend / divisor
with IEEE-754 semantics for division by zero. */
#ifdef __clang__
__attribute__((no_sanitize("float-divide-by-zero")))
#endif
PHP_FUNCTION(fdiv)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recognize the etymology, but I wonder if floatdiv would be a more natural PHP name: it's verbose, and pairs with intdiv obviously. Said another way, the behavior seems closer to intdiv than fmod, so the name should be more closely aligned with the former (floatdiv) than the latter (fdiv).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Said another way, the behavior seems closer to intdiv than fmod, so the name should be more closely aligned with the former (floatdiv) than the latter (fdiv).

That true, but if someone would ask me a question "What is a complementary (?) function to fmod?" my first guess would be fdiv. Just sharing my thoughs 😉

{
double dividend, divisor;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_DOUBLE(dividend)
Z_PARAM_DOUBLE(divisor)
ZEND_PARSE_PARAMETERS_END();

RETURN_DOUBLE(dividend / divisor);
}
/* }}} */

/* {{{ proto int intdiv(int dividend, int divisor)
Returns the integer quotient of the division of dividend by divisor */
PHP_FUNCTION(intdiv)
Expand Down
1 change: 1 addition & 0 deletions ext/standard/php_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ PHP_FUNCTION(octdec);
PHP_FUNCTION(base_convert);
PHP_FUNCTION(number_format);
PHP_FUNCTION(fmod);
PHP_FUNCTION(fdiv);
PHP_FUNCTION(deg2rad);
PHP_FUNCTION(rad2deg);
PHP_FUNCTION(intdiv);
Expand Down
78 changes: 78 additions & 0 deletions ext/standard/tests/math/fdiv.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
--TEST--
fdiv() function
--FILE--
<?php

var_dump(fdiv(10, 3));
var_dump(fdiv(10., 3.));
var_dump(fdiv(-10., 2.5));
var_dump(fdiv(10., -2.5));
echo "\n";
var_dump(fdiv(10., 0.));
var_dump(fdiv(10., -0.));
var_dump(fdiv(-10., 0.));
var_dump(fdiv(-10., -0.));
echo "\n";
var_dump(fdiv(INF, 0.));
var_dump(fdiv(INF, -0.));
var_dump(fdiv(-INF, 0.));
var_dump(fdiv(-INF, -0.));
echo "\n";
var_dump(fdiv(0., 0.));
var_dump(fdiv(0., -0.));
var_dump(fdiv(-0., 0.));
var_dump(fdiv(-0., -0.));
echo "\n";
var_dump(fdiv(INF, INF));
var_dump(fdiv(INF, -INF));
var_dump(fdiv(-INF, INF));
var_dump(fdiv(-INF, -INF));
echo "\n";
var_dump(fdiv(0., INF));
var_dump(fdiv(0., -INF));
var_dump(fdiv(-0., INF));
var_dump(fdiv(-0., -INF));
echo "\n";
var_dump(fdiv(NAN, NAN));
var_dump(fdiv(INF, NAN));
var_dump(fdiv(0., NAN));
var_dump(fdiv(NAN, INF));
var_dump(fdiv(NAN, 0.));

?>
--EXPECT--
float(3.3333333333333)
float(3.3333333333333)
float(-4)
float(-4)

float(INF)
float(-INF)
float(-INF)
float(INF)

float(INF)
float(-INF)
float(-INF)
float(INF)

float(NAN)
float(NAN)
float(NAN)
float(NAN)

float(NAN)
float(NAN)
float(NAN)
float(NAN)

float(0)
float(-0)
float(-0)
float(0)

float(NAN)
float(NAN)
float(NAN)
float(NAN)
float(NAN)