Jump to page: 1 2
Thread overview
overflow
Dec 04, 2003
Tintor Marko
Dec 04, 2003
Tintor Marko
Dec 08, 2003
Walter
Dec 09, 2003
Roberto Mariottini
Dec 09, 2003
Walter
Dec 10, 2003
Georg Wrede
Dec 12, 2003
Roberto Mariottini
Dec 16, 2003
Roberto Mariottini
Dec 16, 2003
Ilya Minkov
Dec 16, 2003
J Anderson
Dec 17, 2003
Roberto Mariottini
Dec 12, 2003
Sean L. Palmer
Dec 17, 2003
Tintor Marko
Dec 17, 2003
Sean L. Palmer
Compile options (was : Re: overflow)
Dec 17, 2003
Patrick Down
December 04, 2003
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
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
"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
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
"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
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
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
"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
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
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
>
>


« First   ‹ Prev
1 2