Board index » delphi » (word)*(word) -> overflow

(word)*(word) -> overflow

Hi all,
How do I solve this overflow problem?

Var   X,Y:word;

Sometimes (seldom) X and Y are about 300 and I get an overflowerror on
this line
IF X*Y>0 then begin....
{ X*Y=300*300=90 000 }

One way is to have them set as LongInt; instead of word;
BUT are there any other ways?

Jan

Re:(word)*(word) -> overflow

Hi Jan!
If you don't want to define X and Y as longint, then try the
following:

Var   X,Y:word;
IF longint( X)*longint(Y)>0 then begin....

regards,
Christoph

On Thu, 28 Jan 1999 10:06:29 +0100, Jan Jacobson

Quote
<jan.jacob...@mbox301.swipnet.se> wrote:
>Hi all,
>How do I solve this overflow problem?

>Var   X,Y:word;

>Sometimes (seldom) X and Y are about 300 and I get an overflowerror on
>this line
> IF X*Y>0 then begin....
>{ X*Y=300*300=90 000 }

>One way is to have them set as LongInt; instead of word;
>BUT are there any other ways?

>Jan

Re:(word)*(word) -> overflow

Jan Jacobson D??? ???Y?? <36B02895.488FD...@mbox301.swipnet.se> ...

Quote
>Hi all,
>How do I solve this overflow problem?

>Var   X,Y:word;

>Sometimes (seldom) X and Y are about 300 and I get an overflowerror on
>this line
> IF X*Y>0 then begin....
>{ X*Y=300*300=90 000 }

>One way is to have them set as LongInt; instead of word;
>BUT are there any other ways?

>Jan

Of Course

use

if LongInt ( X ) * LongInt ( Y ) ...

by the way words are always > 0 and the production of two words is
also > 0.
Sincerily yours,

Re:(word)*(word) -> overflow

On Thu, 28 Jan 1999 10:06:29 +0100, Jan Jacobson

Quote
<jan.jacob...@mbox301.swipnet.se> wrote:
> Hi all,
> How do I solve this overflow problem?

> Var   X,Y:word;

> Sometimes (seldom) X and Y are about 300 and I get an overflowerror on
> this line
>  IF X*Y>0 then begin....
> { X*Y=300*300=90 000 }

Wouldn't in this specific case

if (X=0) or (Y=0) then ...

be more elegant ?

In the general case you might use

if longint(x)*y > 42 then ...

Regards
Horst

Quote
> One way is to have them set as LongInt; instead of word;
> BUT are there any other ways?

> Jan

Re:(word)*(word) -> overflow

In article <36a6fdeb.2490...@news.telekabel.at>,

Quote
Christoph Roschger <christoph.rosch...@usa.net> wrote:
>Hi Jan!
>If you don't want to define X and Y as longint, then try the
>following:

>Var   X,Y:word;
> IF longint( X)*longint(Y)>0 then begin....

It is enough to typecast just one of them:

if longint(x)*y>0

If one us in hurry one can use the following:

Function Wmul(x,y:word):longint;
inline(
\$5B/              {POP     BX}
\$58/              {POP     AX}
\$F7/\$E3           {MUL     BX}
);

But in this case why not do:

if (x>0) and (y>0) then...

Osmo

Re:(word)*(word) -> overflow

Quote
Nickolai Delegnan wrote:
>         by the way words are always > 0 and the production of two words is
> also > 0.

VAR a, b: word;

begin
a:=0;
b:=0;
if NOT (a*b>0) then writeln ('Sorry Nickolai, you''re wrong.');
end.

SCNR. ;-)

Bye,
Ingo

Re:(word)*(word) -> overflow

Quote
Nickolai Delegnan wrote:
> by the way words are always > 0 and the production of two words is also > 0.

>                 Sincerily yours,

Not according to my on-line help.
Word: 0..65535
Jan

Re:(word)*(word) -> overflow

Quote
Horst Kraemer wrote:

Quote
> On Thu, 28 Jan 1999 10:06:29 +0100, Jan Jacobson
> <jan.jacob...@mbox301.swipnet.se> wrote:

> > Hi all,
> > How do I solve this overflow problem?

> > Var   X,Y:word;

> > Sometimes (seldom) X and Y are about 300 and I get an overflowerror on
> > this line
> >  IF X*Y>0 then begin....
> > { X*Y=300*300=90 000 }

> Wouldn't in this specific case

>         if (X=0) or (Y=0) then ...

you must mean if (X<>0) AND (Y<>0) then...?"when both are not zero then...."

Quote
> be more elegant ?

> In the general case you might use

>         if longint(x)*y > 42 then ...

Yes, this also works.Which method do you think is the fastest one?
Is it possible to calculate this?

Regards ,Jan

Re:(word)*(word) -> overflow

Quote

My problem was

Var  X,Y:word;

begin
.....
IF X*Y>0 then.....(but sometimes X=300 and Y=300 )

1.) Longint(X)*Y>0 then ....

2.) IF (X<>0) AND (Y<>0) then...

New question:.......;)
Which of these 2 methods is fastest and/or requires less memory?

Maybe #2 ? as if X<>0 then the program dont continue to do the next
test (Y<>0) or even the AND???

Is it possible to calculate this?

The only method I know of is to make 10-100 miljons simulations and
compare the time?

Regards,Jan

Re:(word)*(word) -> overflow

Sorry guys, what kind of nonsense

WORD ranges from 0..65535
and this is no matter of discussion!
(I guess, understanding of binary-coding lacks...)

and therefore:

VAR x,y:WORD

x*y will alway be positive since
the result of pos*pos can never be
negative! (words cannot hold negatives)

In case of overflow
x:=x*y
x will hold the loword-rest of the
multiplication.

result:=longint(x)*y would be the
fastest and easiest way, i think.

JOE :-)

Re:(word)*(word) -> overflow

I would prefer the 2. because it has
no multiplication.

IF x>0 THEN IF y>0 THEN ....
in case x=0 the second part of
the statement is not evaluated.

JOE :-))

Re:(word)*(word) -> overflow

On 30 Jan 1999 00:50:53 GMT, joe45...@aol.com (JOE45657) wrote:

Quote
>I would prefer the 2. because it has
>no multiplication.

I would prefer the 2. as well. I think it's really faster.

Quote
>IF x>0 THEN IF y>0 THEN ....
>in case x=0 the second part of
>the statement is not evaluated.

Just write:
{\$B-}  {"Complete Boolean Eval OFF"} {is set by default}
if (x>0) and (y>0) then ....

Here, the second part (y>0) isn't evaluated either.

regards,
Christoph

Right!!

JOE :-))

Re:(word)*(word) -> overflow

On 30 Jan 1999 00:37:39 GMT, joe45...@aol.com (JOE45657) wrote:

Quote
>Sorry guys, what kind of nonsense

>WORD ranges from 0..65535
>and this is no matter of discussion!
>(I guess, understanding of binary-coding lacks...)

>and therefore:

>VAR x,y:WORD

>x*y will alway be positive since
>the result of pos*pos can never be
>negative! (words cannot hold negatives)

X*Y can also be zero

Quote
>In case of overflow
>x:=x*y
>x will hold the loword-rest of the
>multiplication.

>result:=longint(x)*y would be the
>fastest and easiest way, i think.

>JOE :-)

A better solution would be to test for both numbers not equal to zero:

If (A <> 0) AND (B <> 0) Then
Begin
End;

This compiles to four instructions (cmp, jbe, cmp, jbe), whereas the
multiplication results in a call to some longint multiply function,
which will definitely take more time.

Of course this simple solution only works when dealing with words. If
you were using integers, the test would have to include the
possibility that one or both numbers are negative.

Regards,
Klaas

Re:(word)*(word) -> overflow

Quote
In article <36b4bf4c.4131...@news.compuserve.com>,  <kdej...@csi.com> wrote:

>A better solution would be to test for both numbers not equal to zero:

>If (A <> 0) AND (B <> 0) Then
>Begin
>End;

>This compiles to four instructions (cmp, jbe, cmp, jbe), whereas the
>multiplication results in a call to some longint multiply function,
>which will definitely take more time.

If is better to test  if (a>0) and (b>0) Then..  That is more readable.

Quote

>Of course this simple solution only works when dealing with words. If
>you were using integers, the test would have to include the
>possibility that one or both numbers are negative.

if (a>0) and (b>0) or (a<0) and (b<0) Then...

In fact one could use the latter with words as well. TP should optimize
the latter part away.

Osmo

Go to page: [1] [2]