Constants for better double edge case handling

php.internals

Anatol Belski

9 years ago
Hi, I would like to suggest adding the following constants REGISTER_MAIN_LONG_CONSTANT("PHP_DBL_DIG", DBL_DIG, CONST_PERSISTENT | CONST_CS); REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MAX", DBL_MAX, CONST_PERSISTENT | CONST_CS); REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MIN", DBL_MIN, CONST_PERSISTENT | CONST_CS); REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_EPSILON", DBL_EPSILON, CONST_PERSISTENT | CONST_CS); The goal of this is to improve the handling of double in the user land. Here are a couple of usages to illustrate the idea. The comparison of double values. $d0 = sin(M_PI/6.0); $d1 = .5; var_dump( $d0, $d1, abs($d0 - $d1), $d0 == $d1, abs($d0 - $d1) < PHP_DBL_EPSILON ); float(0.5) float(0.5) float(5.5511151231258E-17) bool(false) bool(true) The rounding behavior, max possible value representable by the string conversion. $d = .2345234523453245324323465; echo $d, " ", round($d, 20), " ", round($d, PHP_DBL_DIG); 0.23452345234532 0.23452345234532 0.23452345234533 Producing INF. There's currently no explicit way to produce INF and NAN, whereby NAN is gettable with sqrt(-1). echo PHP_DBL_MAX*PHP_DBL_MAX, " ", -PHP_DBL_MAX*PHP_DBL_MAX; INF -INF In general, it is more about the possibility to handle the edge cases properly. While such cases would cause unnecessary overhead and likely a BC breach with a direct core implementation, they'd be fine to handle in the scripts where it comes to it. I think, at least DBL_DIG and DBL_EPSILON should be mapped to the constants, to provide a base for more flexibility. The change itself is pretty outspoken, so I'm not sure it requires an RFC. I would target at least master with this. Or 7.1, if RMs are ok. Regards Anatol

Kalle Sommer Nielsen

9 years ago
Hi Anatol 2016-10-18 20:46 GMT+02:00 Anatol Belski <anatol.php@belski.net>:
> Hi, > > I would like to suggest adding the following constants > > REGISTER_MAIN_LONG_CONSTANT("PHP_DBL_DIG", DBL_DIG, CONST_PERSISTENT | > CONST_CS); > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MAX", DBL_MAX, CONST_PERSISTENT | > CONST_CS); > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MIN", DBL_MIN, CONST_PERSISTENT | > CONST_CS); > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_EPSILON", DBL_EPSILON, > CONST_PERSISTENT | CONST_CS); >
I like this idea but I do think that we should name them PHP_DOUBLE_* or PHP_FLOAT_* (preferably the first).
-- regards, Kalle Sommer Nielsen kalle@php.net

Anatol Belski

9 years ago
Hi Kalle,
> -----Original Message----- > From: kalle.php@gmail.com [mailto:kalle.php@gmail.com] On Behalf Of Kalle > Sommer Nielsen > Sent: Tuesday, October 18, 2016 9:05 PM > To: Anatol Belski <anatol.php@belski.net> > Cc: PHP Internals <internals@lists.php.net>; Davey Shafik <davey@php.net>; Joe > Watkins <krakjoe@php.net> > Subject: Re: [PHP-DEV] Constants for better double edge case handling > > Hi Anatol > > 2016-10-18 20:46 GMT+02:00 Anatol Belski <anatol.php@belski.net>: > > Hi, > > > > I would like to suggest adding the following constants > > > > REGISTER_MAIN_LONG_CONSTANT("PHP_DBL_DIG", DBL_DIG, > > CONST_PERSISTENT | CONST_CS); > > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MAX", DBL_MAX, > > CONST_PERSISTENT | CONST_CS); > > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_MIN", DBL_MIN, > > CONST_PERSISTENT | CONST_CS); > > REGISTER_MAIN_DOUBLE_CONSTANT("PHP_DBL_EPSILON", DBL_EPSILON, > > CONST_PERSISTENT | CONST_CS); > > > > I like this idea but I do think that we should name them PHP_DOUBLE_* or > PHP_FLOAT_* (preferably the first). >
Ah, indeed. PHP_FLOAT_* is what is consistent, probably. Thanks Anatol

Andrew Faulds

9 years ago
Hi Anatol, Anatol Belski wrote:
> Producing INF. There's currently no explicit way to produce INF and NAN, > whereby NAN is gettable with sqrt(-1). > > echo PHP_DBL_MAX*PHP_DBL_MAX, " ", -PHP_DBL_MAX*PHP_DBL_MAX; > INF -INF
I'm not sure I understand this use-case. We already have the INF and NAN constants for obtaining those values, and the standard IEEE 754 operations which produce these values are implemented (1/0 for INF, 0/0 for NAN, etc.)
-- Andrea Faulds https://ajf.me/

Anatol Belski

9 years ago
Hi Andrea,
> -----Original Message----- > From: Andrea Faulds [mailto:ajf@ajf.me] > Sent: Wednesday, October 19, 2016 2:49 PM > To: internals@lists.php.net > Subject: [PHP-DEV] Re: Constants for better double edge case handling > > Hi Anatol, > > Anatol Belski wrote: > > Producing INF. There's currently no explicit way to produce INF and > > NAN, whereby NAN is gettable with sqrt(-1). > > > > echo PHP_DBL_MAX*PHP_DBL_MAX, " ", -PHP_DBL_MAX*PHP_DBL_MAX; INF > -INF > > I'm not sure I understand this use-case. We already have the INF and NAN > constants for obtaining those values, and the standard IEEE 754 operations > which produce these values are implemented (1/0 for INF, 0/0 for NAN,
etc.)
>
Ah, so then it is fine, thanks for the education. Then it'd stay by DBL_DIG and DBL_EPSILON. While DBL_MAX/ DBL_MIN could still be useful in some case (fe one would want to explicitly know the values), in most case it'd be covered by INF. Regards Anatol

Andrew Faulds

9 years ago
Hi, Anatol Belski wrote:
>> Anatol Belski wrote: >>> Producing INF. There's currently no explicit way to produce INF and >>> NAN, whereby NAN is gettable with sqrt(-1). >>> >>> echo PHP_DBL_MAX*PHP_DBL_MAX, " ", -PHP_DBL_MAX*PHP_DBL_MAX; INF >> -INF >> >> I'm not sure I understand this use-case. We already have the INF and NAN >> constants for obtaining those values, and the standard IEEE 754 operations >> which produce these values are implemented (1/0 for INF, 0/0 for NAN, > etc.) >> > Ah, so then it is fine, thanks for the education. Then it'd stay by DBL_DIG > and DBL_EPSILON. While DBL_MAX/ DBL_MIN could still be useful in some case > (fe one would want to explicitly know the values), in most case it'd be > covered by INF.
It can't hurt to add them anyway, though.
-- Andrea Faulds https://ajf.me/