Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.java.programmer,comp.lang.c Subject: Re: Arithmetic overflow checking Date: Sun, 10 Jul 2011 14:47:13 -0700 Organization: None to speak of Lines: 24 Message-ID: References: <015aeb15-57db-48ab-9cd4-77f8448b632f@w24g2000yqw.googlegroups.com> <2rydnez7l-H5BYnTnZ2dnUVZ_vGdnZ2d@earthlink.com> <9LWdnZH2hdfmyYvTnZ2dnUVZ_vidnZ2d@posted.palinacquisition> <3797038f-22d1-40b2-8c12-60db5a0976b8@t5g2000yqj.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx04.eternal-september.org; posting-host="mytEQcPL+ceHcrnNa7VoaQ"; logging-data="26516"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+pP3tu12zzK8Z+bQiJGdI3" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:4FeviF+m9JHNVoCJGzmGo4sf6Vw= sha1:4mcZEWIugXMwb2FGy5J9i8asjeI= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6060 comp.lang.c:8107 Malcolm McLean writes: > On Jul 10, 4:28 pm, Eric Sosman wrote: >>      This is true only for `unsigned' integer arithmetic.  Signed >> integer arithmetic is in fact vulnerable to overflow. >> > All arithmetic is vulnerable to overflow. The question is whether the > system reports and error or gives wrong results. Sometimes wrong > results are better than errors, for instance in video games, but only > rarely. All arithmetic that's intended to model (a subset of) the real numbers with the usual mathematical operations is vulnerable to overflow. (Even arbitrary-precision packages can run out of memory.) But C unsigned arithmetic, for example *doesn't* model normal real arithmetic; it models arithmetic modulo 2**N and is not vulnerable to overflow. (Wraparound is not overflow; it yields the correct wrapped result.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"