Is this bad?

Started by
10 comments, last by DevFred 10 years, 11 months ago

A simple solution to force casting to snap the value is to use a half-unit offset:

iX = (int)(fX + 0.5f);

or to support both positive and negative values:

iX = fX > 0 ? (int)(fX + 0.5f) : (int)(fX - 0.5f);

Note: casting to int truncates towards zero.

Edit: I was too rash - better solutions have been posted above since you're not dealing with unit intervals.

Advertisement

If you want to round an int down to a multiple of 64, you can use bitmasking:


x = x & ~63;

Note that this only works for powers of two.

This topic is closed to new replies.

Advertisement