Bug #73108 [Ver]: Internal class cast handler uses integer instead of float
Edit report at https://bugs.php.net/bug.php?id=73108&edit=1
ID: 73108
Updated by: cmb@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:
Of course, I meant that this issue is related to bug #54973.
Previous Comments:
------------------------------------------------------------------------
[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
Thread (6 messages)