# Object arithmetic

- Thread: https://externals.io/message/32729
- Original archive: https://news-web.php.net/php.internals/32729
- Started by Rob Richards on 2007-10-10T17:37:28+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #32729 — Rob Richards — 2007-10-10T17:37:28+00:00

URL: https://externals.io/message/32729#32729

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

---

## #32840 — Stanislav Malyshev — 2007-10-17T23:10:14+00:00

URL: https://externals.io/message/32729#32840 · In reply to: #32729

> 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

---

## #32842 — Alexey Zakhlestin — 2007-10-18T05:13:51+00:00

URL: https://externals.io/message/32729#32842 · In reply to: #32840

> > […]
>
> 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/

---

## #32853 — Stanislav Malyshev — 2007-10-18T16:08:21+00:00

URL: https://externals.io/message/32729#32853 · In reply to: #32842

> 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

---

## #32862 — Rob Richards — 2007-10-18T20:01:16+00:00

URL: https://externals.io/message/32729#32862 · In reply to: #32840

Stanislav Malyshev wrote:
> > […]
>
> 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
