[vtk-developers] Charts and float/int conversion

David Gobbi david.gobbi at gmail.com
Tue Sep 21 19:11:26 EDT 2010


On Tue, Sep 21, 2010 at 4:53 PM, Jim Peterson <jimcp at cox.net> wrote:
> David Gobbi wrote:
>>
>> inline vtkContext2D::FloatToInt(float x)
>> {
>>  return static_cast<int>(x + 1.0625) - 1;
>> }
>>
>> This provides a tolerance of 1/16 of a pixel for the float-to-int
>> conversion. The "1" and "- 1" ensure that float values in the range
>> [-1.0, 0.0] are rounded down, instead of being rounded toward zero.
>>
>>  David
>
> David,
> pardon my ignorance, but if we are rounding float to int, why do we want
> -0.1 to round to -1 and not 0? in other words, why not x + 1.5 instead of x
> + 1.0625?
>
> Thanks,
> Jim

It all depends on what the rounding is being used for.  When
rasterizing floating-point coordinates to the screen (i.e. to pixels),
it is convention to round down.

The IEEE 754 standard describes five rounding modes, each of which is
valid for its intended uses:
1) Round toward 0, which is C standard float-to-int
2) Round towards -inf, which is like floor()
3) Round towards +inf, which is like ceil()
4) Round to nearest, towards even number if tie, default mode on many CPUs
5) Round to nearest, away from zero if tie

My favorite mode is round to nearest, and towards the higher of the
two integers in case of a tie.  Unfortunately that isn't in the
standard. :P

  David



More information about the vtk-developers mailing list