[Developers] [Patches] fix to integer overflow bug in PUGH

Jonathan Thornburg jthorn at aei.mpg.de
Sat Apr 7 02:09:45 CDT 2007

[[how to print a size_t]]
David Rideout suggested:
> How about casting it to an unsigned long instead?  (Just because in
> principle one may be interested in the exact value.)

On Sat, 7 Apr 2007, Erik Schnetter wrote:
> That works only when you can guarantee that a size_t fits into an unsigned
> long.  This may not be true in general.  You could use unsigned long long
> instead, but this type does not exist on all platforms.
> If the value fits into a double, i.e., if you have less than about 10^15, then
> a double is exact.

C99 <stdint.h> defines the types   intmax_t  and  uintmax_t  for just
this purpose (they're guaranteed to be big enough to hold any
integer/unsigned-integer type in the language standard), with
corresponding C99 printf formats   '%jd   or   %ju   .

What I'm not clear on, however, is whether  intmax_t  is guaranteed
to be big enough to hold a  long long  .

On the other hand, C99 also guarantees that 'long long' and
'unsigned long long' exist (i.e. they're not syntax errors).  On some
lame system they might not be any bigger than 'long' and 'unsigned long'
respectively, though.

Overall, I like Erik's proposal of converting to a  double .
An IEEE double  will hold any integer up to 53 bits exactly
and I strongly suspect that if there are any computers in the world
with 2^53 bytes of addressable memory, they're reserved for spooks
designing H-bombs or pattern-matching every {E-mail,fax,phone call}
in the world, and not available for Cactus.


-- Jonathan Thornburg <jthorn at aei.mpg.de>      
   School of Mathematics, U of Southampton, England
   "Washing one's hands of the conflict between the powerful and the
    powerless means to side with the powerful, not to be neutral."
                                      -- quote by Freire / poster by Oxfam

More information about the Developers mailing list