[racket] Multiplying by 0
At Sun, 13 Feb 2011 16:56:13 +0000,
José Lopes wrote:
> I understand. However, not only that disregards type promotion but also
> is incoherent since (+ 0 0.0) evaluates to 0.0.
It is somewhat inconsistent, and it makes it possible to shoot
yourself in the foot (if you expect the result of the multiplication
to be inexact), but there are reasons why this behavior is useful:
- As Carl said, that makes mathematical sense; anything multiplied by
0 is 0.
- A complex number with an imaginary part of exact 0 can be demoted to
a real number (which use less space and are processed more
efficiently). Therefore, if an imaginary part of exact 0 arises as
the result of a multiplication, you are back to real numbers. This
comes in handy for some computations.
That said, I'm not sure this is worth the inconsistency, but there are
reasons for this decision.
In the meantime, if you have code that depends on the result of a
multiplication of exact and inexact reals being inexact, the safe
thing to do is to wrap the exact number in a call to exact->inexact.
Vincent