<div class="gmail_quote">On Tue, Sep 21, 2010 at 7:11 PM, David Gobbi <span dir="ltr"><<a href="mailto:david.gobbi@gmail.com">david.gobbi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Tue, Sep 21, 2010 at 4:53 PM, Jim Peterson <<a href="mailto:jimcp@cox.net">jimcp@cox.net</a>> wrote:<br>
> David Gobbi wrote:<br>
>><br>
>> inline vtkContext2D::FloatToInt(float x)<br>
>> {<br>
>>  return static_cast<int>(x + 1.0625) - 1;<br>
>> }<br>
>><br>
>> This provides a tolerance of 1/16 of a pixel for the float-to-int<br>
>> conversion. The "1" and "- 1" ensure that float values in the range<br>
>> [-1.0, 0.0] are rounded down, instead of being rounded toward zero.<br>
>><br>
>>  David<br>
><br>
> David,<br>
> pardon my ignorance, but if we are rounding float to int, why do we want<br>
> -0.1 to round to -1 and not 0? in other words, why not x + 1.5 instead of x<br>
> + 1.0625?<br>
><br>
> Thanks,<br>
> Jim<br>
<br>
</div></div>It all depends on what the rounding is being used for.  When<br>
rasterizing floating-point coordinates to the screen (i.e. to pixels),<br>
it is convention to round down.<br>
<br>
The IEEE 754 standard describes five rounding modes, each of which is<br>
valid for its intended uses:<br>
1) Round toward 0, which is C standard float-to-int<br>
2) Round towards -inf, which is like floor()<br>
3) Round towards +inf, which is like ceil()<br>
4) Round to nearest, towards even number if tie, default mode on many CPUs<br>
5) Round to nearest, away from zero if tie<br>
<br>
My favorite mode is round to nearest, and towards the higher of the<br>
two integers in case of a tie.  Unfortunately that isn't in the<br>
standard. :P<br>
<font color="#888888"><br></font></blockquote><div>Thanks for the tips, I had been meaning to look into some of the rounding issues. I will test this out and seeing about getting it in.</div><div><br></div><div>Marcus</div>
</div>