Bug #73108 [Com]: Internal class cast handler uses integer instead of float

From: Date: Thu, 22 Sep 2016 00:33:10 +0000
Subject: Bug #73108 [Com]: Internal class cast handler uses integer instead of float
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-204173@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73108&edit=1 ID: 73108 Comment by: ajf@php.net Reported by: hartenfels at uni-koblenz dot de Summary: Internal class cast handler uses integer instead of float Status: Verified Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 7.0.11 Block user comment: N Private report: N New Comment: As I see it, the root of the problem is we have no way to cast an object to whatever number type is best, we have to request an integer or a float specifically. That means we end up picking one, and I guess we chose integers. We could choose floats, but floats don't work in every situation. What if you need to represent a >54-bit number? I'm not sure how this could be fixed without breaking existing extensions, though. Previous Comments: ------------------------------------------------------------------------ [2016-09-19 13:30:42] cmb@php.net Of course, I meant that this issue is related to bug #54973. ------------------------------------------------------------------------ [2016-09-19 13:29:51] cmb@php.net Indeed, this is related to bug #73108 (might be even seen as duplicate). Note that it's possible to work around that issue by employing an do_operation handler[1] which is available as of PHP 5.6. [1] <https://github.com/php/php-src/blob/PHP-5.6.26/Zend/zend_object_handlers.h#L148> ------------------------------------------------------------------------ [2016-09-19 13:29:51] cmb@php.net Related To: Bug #73108 ------------------------------------------------------------------------ [2016-09-18 21:12:10] hartenfels at uni-koblenz dot de Description: ------------ This is a problem related to cast_object handlers for internal classes. When using an object with a cast handler in arithmetic, integer conversion is triggered instead of floating point conversion, even in situations where floating point math is used. The expected behavior would be that the floating point cast is triggered instead. If only a cast handler for floating point is present, the conversion even fails outright with the message that the object could not be converted to an integer. The cast to double isn't even attempted. This may be related to #54973. Test script: --------------- The entire extension can be found at https://github.com/hartenfels/castbug static int castbug_cast(zval *readobj, zval *retval, int type TSRMLS_DC) { if (type == IS_LONG) { ZVAL_LONG(retval, 123); return SUCCESS; } else if (type == IS_DOUBLE) { ZVAL_DOUBLE(retval, 31.41); return SUCCESS; } else { return FAILURE; } } <?php $cb = new CastBug(); /* these work as expected */ var_dump((int) $cb); var_dump((float) $cb); /* these use integer conversion instead of double conversion */ var_dump($cb + 1.5); var_dump($cb * 2.0); var_dump($cb / 10.0); Expected result: ---------------- int(123) float(31.41) float(32.91) float(62.82) float(3.141) Actual result: -------------- int(123) float(31.41) float(124.5) float(246) float(12.3) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73108&edit=1

« previous php.bugs (#204173) next »