Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 04, 2003 overflow | ||||
---|---|---|---|---|
| ||||
How to check for overflow in integer operations? void main() { int a,b,c; scanf("%ld %ld %ld", &a, &b, &c); int d=a*b+c; if(overflow) printf("error\n"); else printf("%ld",d); } |
December 04, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tintor Marko | On Thu, 04 Dec 2003 10:48:58 +0100, Tintor Marko <elud@verat.net> wrote:
> How to check for overflow in integer operations?
>
> void main()
> {
> int a,b,c;
> scanf("%ld %ld %ld", &a, &b, &c);
> int d=a*b+c;
> if(overflow) printf("error\n"); else printf("%ld",d);
> }
>
or something like:
void main()
{
int a,b,c;
scanf("%ld %ld %ld", &a, &b, &c);
try printf("%ld",a*b+c); catch(OverflowException) printf("error");
}
Can overflow chacking be done in D?
|
December 08, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tintor Marko | "Tintor Marko" <elud@verat.net> wrote in message news:oprznpyvm1lucekh@news.digitalmars.com... > On Thu, 04 Dec 2003 10:48:58 +0100, Tintor Marko <elud@verat.net> wrote: > > > How to check for overflow in integer operations? > > > > void main() > > { > > int a,b,c; > > scanf("%ld %ld %ld", &a, &b, &c); > > int d=a*b+c; > > if(overflow) printf("error\n"); else printf("%ld",d); > > } > > > > or something like: > > void main() > { > int a,b,c; > scanf("%ld %ld %ld", &a, &b, &c); > try printf("%ld",a*b+c); catch(OverflowException) printf("error"); > } > > Can overflow chacking be done in D? Not directly in the language any more than it can be done in C. You could do it with inline assembler, though. |
December 09, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <br15np$834$1@digitaldaemon.com>, Walter says... > > >"Tintor Marko" <elud@verat.net> wrote in message news:oprznpyvm1lucekh@news.digitalmars.com... [...] >> Can overflow chacking be done in D? > >Not directly in the language any more than it can be done in C. You could do it with inline assembler, though. What are the difficulty to do it in the language? Why no language I know give the opportunity to catch overflows, while all CPU (thus assembly) do? Ciao |
December 09, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | "Roberto Mariottini" <Roberto_member@pathlink.com> wrote in message news:br46c8$1q5j$1@digitaldaemon.com... > In article <br15np$834$1@digitaldaemon.com>, Walter says... > > > > > >"Tintor Marko" <elud@verat.net> wrote in message news:oprznpyvm1lucekh@news.digitalmars.com... > [...] > >> Can overflow chacking be done in D? > > > >Not directly in the language any more than it can be done in C. You could do > >it with inline assembler, though. > > What are the difficulty to do it in the language? > Why no language I know give the opportunity to catch overflows, while all CPU > (thus assembly) do? Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. |
December 10, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <br52km$7da$1@digitaldaemon.com>, Walter says... >"Roberto Mariottini" <Roberto_member@pathlink.com> wrote in message news:br46c8$1q5j$1@digitaldaemon.com... >> In article <br15np$834$1@digitaldaemon.com>, Walter says... >> >"Tintor Marko" <elud@verat.net> wrote in message news:oprznpyvm1lucekh@news.digitalmars.com... >> [...] >> >> Can overflow chacking be done in D? >> >Not directly in the language any more than it can be done in C. You could do it with inline assembler, though. >> What are the difficulty to do it in the language? >Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. Hmm, isn't it more because of perceived performance issues? In non-release code this would be quite a welcome thing. We would not even need any syntax, integer overflow could throw an Error. |
December 12, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <br52km$7da$1@digitaldaemon.com>, Walter says... > [...] >Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. What about: try { .. x = checkoverflow ( y * w + z ); .. } catch (OverflowError oe) { .. } The "checkoverflow" keyword will throw an OverflowError if the expression generates an integer overflow (the carry/overflow flag is on). Note: - Any better idea about the "checkoverflow" name?. - Can it be an ordinary function (implemented in assembly)? - It should be limited to integers or can be extended to floating points? - What about a "try" extension? i.e.: overflow { .. } catch(...) .. - And a version-like statement? i.e.: // begin of file overflow { // here all integer calculations throw an OverflowError .. // the whole file skipped } // end of file Ciao |
December 12, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <walter@digitalmars.com> wrote in message news:br52km$7da$1@digitaldaemon.com... > > Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. > And why not: try { x = a + b; } catch (OverflowException oe) { if (oe.carry) (...) } |
December 12, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio César Carrascal Urquijo | Because integer overflow is not normally an exceptional condition. Not to mention, why would you test an OverflowException for a carry flag? It seems if the exception is thrown, you wouldn't have to test the flag. However I wouldn't want the compiler looking for overflows all the time. Doesn't overflow bit stick around until it is cleared? It might be better to put this kind of requirement into the type. If we did that, we'd end up with this variety of ints in the type system: int_modular // (the current int, no exception, wraps around to other side) int_checked // (throws exception on overflow or wrap) int_saturate // (prevents overflow by saturating, but doesn't generate exception) Sean "Julio César Carrascal Urquijo" <adnoctum@phreaker.net> wrote in message news:brcu9j$84$1@digitaldaemon.com... > > "Walter" <walter@digitalmars.com> wrote in message news:br52km$7da$1@digitaldaemon.com... > > > > Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. > > > > And why not: > > try > { > x = a + b; > } > catch (OverflowException oe) > { > if (oe.carry) > (...) > } |
December 16, 2003 Re: overflow | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roberto Mariottini | I realized that maybe an ordinary function like checkoverflow can be already done with inline assembly. Is it true? Ciao In article <brc43p$1r8d$1@digitaldaemon.com>, Roberto Mariottini says... > >In article <br52km$7da$1@digitaldaemon.com>, Walter says... >> >[...] >>Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag. > >What about: > >try >{ >.. >x = checkoverflow ( y * w + z ); >.. >} >catch (OverflowError oe) >{ >.. >} > >The "checkoverflow" keyword will throw an OverflowError if the expression generates an integer overflow (the carry/overflow flag is on). > >Note: >- Any better idea about the "checkoverflow" name?. >- Can it be an ordinary function (implemented in assembly)? >- It should be limited to integers or can be extended to floating points? >- What about a "try" extension? i.e.: >overflow >{ >.. >} >catch(...) >.. >- And a version-like statement? i.e.: >// begin of file >overflow >{ >// here all integer calculations throw an OverflowError >.. // the whole file skipped >} >// end of file > >Ciao > > |
Copyright © 1999-2021 by the D Language Foundation