[Libreoffice] [PATCH] fix for crash in math.cxx for std::numeric_limits<int>::min

Lionel Elie Mamane lionel at mamane.lu
Mon Dec 12 22:33:05 PST 2011


On Tue, Dec 13, 2011 at 12:50:57AM -0500, Kohei Yoshida wrote:
> On Tue, 2011-12-13 at 00:15 +0100, Markus Mohrhard wrote:

>> Any objections against this patch? I think that this fix should be
>> correct for all cases but it would be good if someone could have a
>> look at it.

> Well, I'm not necessarily objecting, but I'm a bit puzzled.  I
> would've thought that, if nExp < 0 is true, -nExp > 0 is
> automatically true.

For a signed integer in twos complement, that is true, except for the
smallest representable (negative) number; it stems from the asymmetry
of the range.

In n bits, you can represent from -2^(n-1) to 2^(n-1)-1. Note how
-2^(n-1) is an outlier in the symmetry with respect to 0; (lambda
x.-x) (the function that takes x as an argument and returns -x, the
opposite of x) is effectively the symmetry with centre point 0. But
-(-2^(n-1)), that is 2^(n-1) is not representable. So it wraps around
back to -2^(n-1), like if you would add 1 to 2^(n-1)-1. And that's
actually exactly what happens.

See http://en.wikipedia.org/wiki/Two%27s_complement#The_most_negative_number

-- 
Lionel


More information about the LibreOffice mailing list