D - overflow
- Tintor Marko (8/8) Dec 04 2003 How to check for overflow in integer operations?
- Tintor Marko (9/17) Dec 04 2003 or something like:
- Walter (4/23) Dec 07 2003 Not directly in the language any more than it can be done in C. You coul...
- Roberto Mariottini (6/11) Dec 09 2003 What are the difficulty to do it in the language?
- Walter (6/19) Dec 09 2003 do
- Georg Wrede (4/16) Dec 10 2003 Hmm, isn't it more because of perceived performance issues?
- Roberto Mariottini (35/37) Dec 12 2003 What about:
- Roberto Mariottini (4/42) Dec 16 2003 I realized that maybe an ordinary function like checkoverflow can be alr...
- Ilya Minkov (11/13) Dec 16 2003 No, since you cannot rely upon execution order, and the compiler is free...
- J Anderson (4/18) Dec 16 2003 Parhaps someone could just define add(), mul(), sub() functions which
- Roberto Mariottini (6/18) Dec 17 2003 So you can have:
- Julio César Carrascal Urquijo (12/14) Dec 12 2003 And why not:
- Sean L. Palmer (15/31) Dec 12 2003 Because integer overflow is not normally an exceptional condition. Not ...
- Tintor Marko (16/34) Dec 17 2003 How about this syntax for compiler directives (similar to atributes)?
- Sean L. Palmer (5/21) Dec 17 2003 I would like having this much control. And a standard way to set option...
- Patrick Down (3/28) Dec 17 2003 I would second this. I would like a way to turn array bounds checking
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); }
Dec 04 2003
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?
Dec 04 2003
"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:Not directly in the language any more than it can be done in C. You could do it with inline assembler, though.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?
Dec 07 2003
In article <br15np$834$1 digitaldaemon.com>, Walter says..."Tintor Marko" <elud verat.net> wrote in message news:oprznpyvm1lucekh news.digitalmars.com...[...]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? CiaoCan 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.
Dec 09 2003
"Roberto Mariottini" <Roberto_member pathlink.com> wrote in message news:br46c8$1q5j$1 digitaldaemon.com...In article <br15np$834$1 digitaldaemon.com>, Walter says...do"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 couldCPUit 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(thus assembly) do?Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag.
Dec 09 2003
In article <br52km$7da$1 digitaldaemon.com>, Walter says..."Roberto Mariottini" <Roberto_member pathlink.com> wrote in message news:br46c8$1q5j$1 digitaldaemon.com...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.In article <br15np$834$1 digitaldaemon.com>, Walter says...Good question. The answer is probably the difficulty in coming up with a sensible syntax for the carry flag."Tintor Marko" <elud verat.net> wrote in message news:oprznpyvm1lucekh news.digitalmars.com...[...]What are the difficulty to do it in the language?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.
Dec 10 2003
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
Dec 12 2003
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
Dec 16 2003
Roberto Mariottini wrote:I realized that maybe an ordinary function like checkoverflow can be already done with inline assembly. Is it true?No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting. -eye
Dec 16 2003
Ilya Minkov wrote:Roberto Mariottini wrote:Parhaps someone could just define add(), mul(), sub() functions which could *either* return an exception or be written like: int add(int l, int r, out bool overflow);I realized that maybe an ordinary function like checkoverflow can be already done with inline assembly. Is it true?No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting. -eye
Dec 16 2003
In article <brndrl$1dnt$1 digitaldaemon.com>, Ilya Minkov says...Roberto Mariottini wrote:So you can have: int x = oguard(x + oguard(y * z)); If oguard is inline expanded, I think it will do the job. Do you have an idea of a possible implementation? CiaoI realized that maybe an ordinary function like checkoverflow can be already done with inline assembly. Is it true?No, since you cannot rely upon execution order, and the compiler is free to insert any stuff between the statements it likes, which is very likely to zero the flags. The syntax must thus imply which operation *exactly* has to be checked. I would guess something like int bla = overloadguard(expr); bla gets assigned the result of expression, and the outermost operation is checked in expr against overflow. Or maybe only allow expr to be an atomic expression, do not allow nesting.
Dec 17 2003
"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) (...) }
Dec 12 2003
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) (...) }
Dec 12 2003
How about this syntax for compiler directives (similar to atributes)? directive = (enable | disable) "(" identifier ")" (command | block | ":") example: enable(IntOverflowCheck) { int func(int a, int b, int c) { b++; // checking disable(IntOverflowCheck) a*=2; // not checking return a*b+c; // checking } } or like this: check(ArrayIndex) {} uncheck(ArrayIndex) {} check(IntOverflow) {}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?
Dec 17 2003
I would like having this much control. And a standard way to set options within the language would be nice... Sean "Tintor Marko" <elud verat.net> wrote in message news:opr0cfaxdhlucekh news.digitalmars.com...How about this syntax for compiler directives (similar to atributes)? directive = (enable | disable) "(" identifier ")" (command | block | ":") example: enable(IntOverflowCheck) { int func(int a, int b, int c) { b++; // checking disable(IntOverflowCheck) a*=2; // not checking return a*b+c; // checking } } or like this: check(ArrayIndex) {} uncheck(ArrayIndex) {} check(IntOverflow) {}
Dec 17 2003
I would second this. I would like a way to turn array bounds checking on and off. In article <brql4n$mck$1 digitaldaemon.com>, Sean L. Palmer says...I would like having this much control. And a standard way to set options within the language would be nice... Sean "Tintor Marko" <elud verat.net> wrote in message news:opr0cfaxdhlucekh news.digitalmars.com...How about this syntax for compiler directives (similar to atributes)? directive = (enable | disable) "(" identifier ")" (command | block | ":") example: enable(IntOverflowCheck) { int func(int a, int b, int c) { b++; // checking disable(IntOverflowCheck) a*=2; // not checking return a*b+c; // checking } } or like this: check(ArrayIndex) {} uncheck(ArrayIndex) {} check(IntOverflow) {}
Dec 17 2003