Board index » delphi » (no subject)

(no subject)

begin
        {$Q-}
         closegraph;
         {Why does this calculation return a runtime error 207 }
         {(Arithmetic overflow)}
         radii :=
round(sqrt(sqr(center.x-mpos.mx)+sqr(center.y-mpos.my)));
         {radii = longint, center.x(y) and mpos.mx(y) = integer}

PLEASE!
Can anyone reply to this question on this address:
kgjen...@stud.mdb.ku.dk

 

Re:(no subject)


Re:(no subject)


Quote
Karsten Gjessing Jensen wrote:

> begin
>         {$Q-}
>          {Why does this calculation return a runtime error 207 }
>          {(Arithmetic overflow)}
>          radii :=
> round(sqrt(sqr(center.x-mpos.mx)+sqr(center.y-mpos.my)));
>          {radii = longint, center.x(y) and mpos.mx(y) = integer}

sqr returns a result of the same type as the parameter, which as stated
above is integer. 500*500 = 250,000 and this is outside the range of
integer. Such cases are liable to result in (what is interpreted as)
a negative value, and the overflow then occurs in sqrt. Even if not
interpreted as negative, it will likely be interpreted wrongly.

Present a longint instead of an integer to sqr and you'll be all right.
Various possibilities, eg center.x etc could be longint, or even

 radii :=
 round(sqrt(sqr(longint(center.x-mpos.mx))+sqr(longint(center.y-mpos.my))));

if you really like piling up the stack <g>.

PS: Please give your posting a subject indicating the topic of your question.

Re:(no subject)


Re:(no subject)


Karsten Gjessing Jensen <Kgjen...@mdb.ku.dk> wrote in article
<3427A7B3.2...@mdb.ku.dk>...

Quote
> begin
>         {$Q-}
>          closegraph;
>          {Why does this calculation return a runtime error 207 }
>          {(Arithmetic overflow)}
>          radii :=
> round(sqrt(sqr(center.x-mpos.mx)+sqr(center.y-mpos.my)));
>          {radii = longint, center.x(y) and mpos.mx(y) = integer}

> PLEASE!
> Can anyone reply to this question on this address:
> kgjen...@stud.mdb.ku.dk

You are mixing integer and real arithmetic operations.  SQR returns a value
of the same type as its parameter so you will get integer values.
Therefore something like SQR(200) will return -25536!!! (40000).  Thus when
you find SQRT(-ve number) you get the error.

Try this:
  radii := round( sqrt( sqr(LONGINT(center.x - mpos.mx)) +
                        sqr(LONGINT(center.y - mpos.my))));

Cheers,
Lester

Other Threads