[geeks] Quick C question

Jonathan C. Patschke jp at celestrion.net
Wed Jul 24 23:02:37 CDT 2002


On Wed, 24 Jul 2002, Joshua D Boyd wrote:

> In the belief that good code should compile without even warnings, I
> can't figure out how to make G++ stop warning about this line:
>
> s = s * 2891336453 + 1;

The problem is that 2891336453 won't fit in 31 bits, so the compiler is
telling you that it inferred a type based on its content, and that type
might not be what you intended.

To tell it that you're okay with that constant being an unsigned int (as
opposed to the assumed default of int), append a U to the end of the
constant, like this:

s = s * 2891336453U + 1;

It's also a warning that your multiply and add will probably result in
overflow (if s is an int) or loss of significant digits (if s is a float).

Also, just as a matter of style, 2891336453 is a magic number.  That is,
it doesn't mean anything out of context.  Consider making a symbolic
#define for that constant that would make that calculation more
meaningful.

-- 
Jonathan Patschke
  "gnu: we aim to fuck up everything with the potential to not suck"
                                                   --alex j avriette



More information about the geeks mailing list