← Back to team overview

kicad-developers team mailing list archive

Re: Rationale for rounding BIUs


On Sun, Jun 24, 2012 at 06:23:46PM +0200, jean-pierre charras wrote:
> I am afraid I do not really understand your question.
> In float to integer conversions, rounding creates a smaller error than truncating.
> And truncating fval < 0 ? fval - 0.5 : fval + 0.5 is the right way to round a value that must be converted to integer BIU.

Right, I was asking before in other circumstances (i.e. motion control)
truncation or rounding is often (I can't say always, but almost)
replaced by floor()ing, to avoid a discontinuity issue around zero
(reason: the default rounding minimized biasing, flooring minimizes
discontinuities, more or less; I don't know the proper math terms...)
I generally simply avoid floating point if possible using fixed point
instead, if dynamic range is not needed; there is also the issue of the
rounding mode in use and so on (wikipedia treats everything in

In short I asked because I have simply never seen that construction (but
in fact maybe it's simply equivalent to floor(x+0.5) :P). Just to know
better the code.

> By the way, your recent issue when reading your board file in kicad nanometer version is a truncating issue,
> when scaling float values read in files to integer internal units.
> When using rounding conversion instead of truncating, it happens no more.

Wasn't that a precision issue caused by internal rounding of
printf/scanf using doubles instead of long doubles? (I would have simply
saved the nanos as integer...) If that was the case changing precision
shouldn't have changed the result!

Anyway, this is my view about float to int conversion: if possible,
avoid it; otherwise just do whatever works (not very scientific,
I know...)

Lorenzo Marcantonio
Logos Srl

Follow ups