Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #59351

Re: Test for overflow

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c, sci.math.symbolic
Subject Re: Test for overflow
Date 2015-03-31 13:58 -0400
Organization Self
Message-ID <551AE057.1000603@verizon.net> (permalink)
References (1 earlier) <551985CA.7000409@verizon.net> <5519DFAA.40505@verizon.net> <mfdaal$s1j$1@speranza.aioe.org> <551ADC38.4010704@verizon.net> <mfemm4$amt$1@speranza.aioe.org>

Cross-posted to 2 groups.

Show all headers | View raw


On 03/31/2015 01:48 PM, jacob navia wrote:
> Le 31/03/2015 19:41, James Kuyper a écrit :
>> I think so, so long as 'digit' is no wider than 'value'
> 
> "Value" is an unsigned long long. To be able to overflow by adding a 
> number smaller than 37, "value" *must* be bigger than 2^64 - 37.
> 
> If an overflow occurs, at most the last 6 bits could be set, what is 
> surely smaller than a number that is bigger thzn 2^64 - 37!!

The issue isn't the value of 'digit', but it's type. In the extremely
unlikely event that it was wider than unsigned long long (say,
int128_t), then 'value' would be converted to that wider type in both
places in the expression "value + digit < value". As a result, that
expression would detect overflow based upon the maximum value of the
wider type, producing false negatives if what you're really looking for
is overflow of the narrower type.
That's all based upon the possibility that 'digit' was wider than
'value', which I explicitly acknowledged was extremely implausible in
context. The purpose for which it's used could be served by unsigned
char, though unsigned int or unsigned long long would probably generate
faster code on many platforms. I just mentioned that possibility only
for the sake of completeness, since I had not seen any actual declarations.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Test for overflow jacob navia <jacob@jacob.remcomp.fr> - 2015-03-30 13:17 +0200
  Re: Test for overflow Lőrinczy Zsigmond <zsiga@nospam.for.me> - 2015-03-30 13:34 +0200
  Re: Test for overflow Richard Damon <Richard@Damon-Family.org> - 2015-03-30 07:42 -0400
    Re: Test for overflow asetofsymbols@gmail.com - 2015-03-30 04:47 -0700
      Re: Test for overflow jacob navia <jacob@jacob.remcomp.fr> - 2015-03-30 13:57 +0200
        Re: Test for overflow Richard Damon <Richard@Damon-Family.org> - 2015-03-30 08:24 -0400
  Re: Test for overflow Richard Heathfield <rjh@cpax.org.uk> - 2015-03-30 13:03 +0100
  Re: Test for overflow Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-30 06:54 -0700
  Re: Test for overflow James Kuyper <jameskuyper@verizon.net> - 2015-03-30 13:20 -0400
    Re: Test for overflow James Kuyper <jameskuyper@verizon.net> - 2015-03-30 13:32 -0400
    Re: Test for overflow James Kuyper <jameskuyper@verizon.net> - 2015-03-30 19:43 -0400
      Re: Test for overflow jacob navia <jacob@jacob.remcomp.fr> - 2015-03-31 07:11 +0200
        Re: Test for overflow James Kuyper <jameskuyper@verizon.net> - 2015-03-31 13:41 -0400
          Re: Test for overflow jacob navia <jacob@jacob.remcomp.fr> - 2015-03-31 19:48 +0200
            Re: Test for overflow James Kuyper <jameskuyper@verizon.net> - 2015-03-31 13:58 -0400
  Re: Test for overflow Thomas Jahns <jahns@idontlikespam.dkrz.de> - 2015-04-01 10:12 +0200
    Re: Test for overflow Richard Heathfield <rjh@cpax.org.uk> - 2015-04-01 09:43 +0100
      Re: Test for overflow Thomas Jahns <jahns@idontlikespam.dkrz.de> - 2015-04-01 10:59 +0200
        Re: Test for overflow Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-04-01 04:07 -0700
          Re: Test for overflow raltbos@xs4all.nl (Richard Bos) - 2015-04-01 13:52 +0000

csiph-web