Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!usenet.ukfsn.org!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer,comp.lang.c Subject: Re: Arithmetic overflow checking Date: Tue, 12 Jul 2011 16:54:36 +0000 (UTC) Organization: UK Free Software Network Lines: 43 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> <289ad570-65fc-49d8-9cc8-1f15d13ff3e3@gv8g2000vbb.googlegroups.com> NNTP-Posting-Host: 84.45.235.129 Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: localhost.localdomain 1310489676 31725 84.45.235.129 (12 Jul 2011 16:54:36 GMT) X-Complaints-To: usenet@localhost.localdomain NNTP-Posting-Date: Tue, 12 Jul 2011 16:54:36 +0000 (UTC) User-Agent: Pan/0.133 (House of Butterflies) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6123 comp.lang.c:8178 On Tue, 12 Jul 2011 09:26:49 -0700, markspace wrote: > On 7/12/2011 6:16 AM, Stefan Ram wrote: >> "BartC" writes: >>> a+b overflows, but then what? >> >> This can only be answered given the requirements specification of a >> specific project. > > > What I think he's saying is there's no way physically detect the > overflow in a language like C which has no exceptions. You'd have to at > least introduce some sort of global flag. > > int c = a + b; > if( GLOBAL_OVERFLOW_FLAG ) { > printf( "bugger..." ); > } Interestingly, the much maligned COBOL has a built-in mitigation for overflow and a way of trapping it: Mitigation: since you can specify the number of digits in a numeric data item (integer or fixed decimal), it follows that the programmer can control whether overflow can occur and, as the maximum size of a numeric field is 18 digits, the result field can almost alrays be made long enough. Trapping: all computational verbs will accept the ON SIZE ERROR clause which, despite its name, traps almost all possible calculation errors including overflow, divide by zero, etc.: ADD TRANSACTION-VALUE TO TOTAL-VALUE ON SIZE ERROR PERFORM REPORT-CALCULATION-ERROR. ...and yeah, whoever it was who said Java was verbose has clearly never written COBOL! -- martin@ | Martin Gregorie gregorie. | Essex, UK org |