Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Richard Heathfield <rjh@cpax.org.uk> |
|---|---|
| Newsgroups | comp.lang.c, sci.math.symbolic |
| Subject | Re: Test for overflow |
| Date | 2015-03-30 13:03 +0100 |
| Organization | go, ion artisan! |
| Message-ID | <mfbe0a$6pp$1@dont-email.me> (permalink) |
| References | <mfbbc6$9h9$3@speranza.aioe.org> |
Cross-posted to 2 groups.
On 30/03/15 12:17, jacob navia wrote:
> Hi the math experts!
>
> When parsing a digit sequence I test for overflow not by calculating the
> maximum number of digits allowed but just with the test
>
> tvalue = value * base;
>
> if (tvalue < value) {
> // overflow detected
> }
>
> I am assuming then that
>
> if value * base > 2^64 then
> value * base mod 2^64 < value
>
> for base >= 2 and base <= 36.
>
> Is this correct?
Consider simpler cases. For example, if we have a four-bit type, then
for overflow we are concerned by the following cases (we start off by
ignoring 0 * anything and 1 * anything and 2 * stuff that won't overflow):
2 * 8 = 16 = 0 mod 16 - fine
3 * 8 = 24 = 8 mod 16 - this would fail your test, but it's right on the
edge
4 * 8 = 32 = 0 mod 16 - fine
5 * 8 = 40 = 8 mod 16 - fail, and we have a pattern for 8s
2 * 9 = 18 = 2 mod 16 - fine
3 * 9 = 27 = 11 mod 16 - fail, and not really a borderline fail either
and the theory seems to be running into problems, even with four-bit
quantities.
Okay, I grant that 64 bits != 4 bits. But if it doesn't work for 4, I
see no particular reason to be optimistic that it'll work for 64.
--
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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