Subject: Re: Castings...
To: Neil A. Carson <neil@causality.com>
From: Eduardo E. Horvath <eeh@one-o.com>
List: tech-kern
Date: 10/09/1997 08:33:45
On Wed, 8 Oct 1997, Neil A. Carson wrote:

> Chris G. Demetriou wrote:
> > 
> > > Situation:
> > >       1 Casting a negative floating point number to a unsigned integer
> > > also
> > >       2   12 % -5 (both long longs)
> 
> Are there any defined standards for this stuff? In java, they are
> defined, is there a 'C' equivalent? Java says if it's negative you get
> zero as the result; should this be the defined behaviour in 'C' too?
> 
> A similar case for casting out of range numbers was also encountered.

Since nobody else responded to this I'll give it a try.  Since I don't
have the C specification I'll use the C++ ARM.  I beleive this is one
place where the two standards agree.

It says that "(a/b)*b+a%b is equal to a.  If both operands are nonnegative
then the remainder is nonnegative; if not, the sign of the remainder is
implementation dependent."  It goes on to note that the reason for this
undefined behavior is compatibility with C.

Since C will map this operation directly to a machine instruction when
possible, it is inherently machine dependent.  However, you shouldn't be
getting different results for `int' and `long long' unless you have
integer overflow.

=========================================================================
Eduardo Horvath				eeh@btr.com
"Cliffs are for climbing.  That's why God invented grappling hooks."
					- Benton Frasier