Floating Point: are round(x-y) and -round(y-x) equal?

396 Views Asked by At

I think they should intuitively be equal. But I don't know how to show it. And I can't find a counter example to it either to disprove it.

2

There are 2 best solutions below

2
On BEST ANSWER

It depends on how you round half-odd-integers. If you use the "round half up" rule, $\text{round}(1/2) = 1$ but $\text{round}(-1/2) = 0$.

1
On

In general, this is not the same. Choose $x=1$ and $y=0.5$, for example.

But if the difference is not of the form $n+0.5$ with integer $n$, the two expressions give the same value.