Christopher Key wrote:

> I have some code that needs to perform signed division by a power of two
> with rounding towards minus infinity, i.e. it requires an arithmetic
> right shift.  Now in the C specification, right shifting a signed
> integer is implementation defined. 
Because C may be compiled for ints other than 2's complement.  Perhaps
you could use pre-processing to specialize for 2's complement for most
of your targets.

> On a similar point, is there a good way get floats rounded to the
> nearest integer value rather than truncated.  The following give the
> correct rounding behaviour (I'm only interested in +ve values),
> 
> x = (int) (f + 0.5)
> 
I don't see how this is similar, unless you are harking back to the
early days of BASIC when there were 2's complement floating point
implementations.  IEEE standards ruled that out over 20 years ago.  Even
among those 2's complement implementations which did exist, the results
varied for -1. < f < -.5.  If you use this only for positive f, the case
 for which most people would consider this wrong is where f takes odd
integral values, so your code will increment the result.  But you said
this is OK, so where is the problem?

Why not use the lrint() and related C intrinsics?

Reply via email to