I was wondering why objects are always converted to longs when
performing arithmetic.
While looking at bug 42780: http://bugs.php.net/bug.php?id=42780
The only way not to lose precision is to explicitly cast the object to
float or string, which just doesn't seem right to me.
It would be nice if there was an object handler like to_numeric where an
object could determine what type of numeric to return, or some other way
for this to happen. This would then allow it to work like performing
math on strings. When is_numeric string is called, it is properly to
converted to either a double or long so precision is not lost.
Rob
While looking at bug 42780: http://bugs.php.net/bug.php?id=42780
The only way not to lose precision is to explicitly cast the object to
float or string, which just doesn't seem right to me.
I'm not sure how this can be fixed - there should be some type used and
many objects won't provide convertor to float... Anyway, it's probably
not the best idea to rely on default conversion in this context - better
to specify it.
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
While looking at bug 42780: http://bugs.php.net/bug.php?id=42780
The only way not to lose precision is to explicitly cast the object to
float or string, which just doesn't seem right to me.I'm not sure how this can be fixed - there should be some type used and
many objects won't provide convertor to float... Anyway, it's probably
not the best idea to rely on default conversion in this context - better
to specify it.
In this case, the proper solution is to force SimpleXML to cast values
to strings, because, after all, XML has nothing but strings. Then
engine would be able to cast the value further to float (as the last
example in bug shows)
--
Alexey Zakhlestin
http://blog.milkfarmsoft.com/
In this case, the proper solution is to force SimpleXML to cast values
to strings, because, after all, XML has nothing but strings. Then
- operator requires numeric values. SimpleXML can't return string when
asked for numeric value. The problem is while + can accept both int and
float, it can't ask for both.
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com
Stanislav Malyshev wrote:
While looking at bug 42780: http://bugs.php.net/bug.php?id=42780
The only way not to lose precision is to explicitly cast the object
to float or string, which just doesn't seem right to me.I'm not sure how this can be fixed - there should be some type used
and many objects won't provide convertor to float... Anyway, it's
probably not the best idea to rely on default conversion in this
context - better to specify it.
I would have agreed 100% that explicit casting should be used, but with
to_string around, it just doesn't seem right now.Take the following:
$xml = '<root><a>1.1</a><b>2.2</b></root>';
$sxe = simplexml_load_string($xml);
print $sxe->a ." + ".$sxe->b." = ".($sxe->a +$sxe->b);
1.1 + 2.2 = 3
And why wouldn't objects provide a float converter? If they provided a
converter to long why wouldn't they also provide means to convert to float.
Rob