4 ms·
Can an interpreter shirt curcuit a multiplication if the left side is 0? In Python, for instance, is the following: a * b * c * d computed like this?:
by mangeletti 10y ago
Can an interpreter shirt curcuit a multiplication if the left side is 0? In Python, for instance, is the following:
a * b * c * d
computed like this?:
((a * b) * c) * d
Or is it computed like this?:
a * ((b * c) * d)
If the former is the case, then the chongli optimization could help, right?
- palunon 10y agoIf you have IEEE compliant floats, you can't, because the right side may result in Infinity, making the complete product NaN.
- mangeletti 10y agoInteresting. We're talking integers here, though (re: Python).
- chrisseaton 10y agoShort circuiting means branching, which is likely to be slower than running operators that you know to result is zero.
- nayuki 10y agoNo, an interpreter cannot short-circuit a zero multiplication. This is because the expressions could be functions that raise exceptions. e.g. 0 * (0/0) e.g. f() * raise_some_exception()
- mangeletti 10y agoAh, of course. In hindsight, I now feel pretty silly even asking that.