[cairo] Banker's rounding problems and Nautilus

Bill Spitzak spitzak at d2.com
Fri Dec 1 18:25:08 PST 2006

Carl Worth wrote:

 > I haven't looked at the nautilus source code, (I'm curious to know why
 > it's apparently passing non-integer glyph spacings to cairo).

I think the problem is that code that wants to center things will 
typically divide by 2, whether or not the number is odd, even, positive, 
or negative. This makes i+.5 coordinates much more likely than other 
fractions, so they have to be handled correctly.

 > Another idea that Behdad had was to do the magic-number approach, but
 > with one sub-integer bit, which we could then round as we see fit. I
 > would imagine that we could ensure that no callers of _cairo_lround
 > need more than 31 bits, but I haven't checked that yet.

I agree that a fast solution would be to use the magic constant add to 
convert to a 31.1 fixed format, then right shift (not divide!) the 
result by 1. The constant should be increased by .25, and the magic 
converter will then round to the nearest .5. This means that values from 
.5 to 1 (inclusive) will round to 1. Values greater than 0 and less than 
.5 will round to .5 which will then be right-shifted to zero.

More information about the cairo mailing list