Weird maths (sqrt) 
Author Message
 Weird maths (sqrt)

I am getting stupid results in a procedure that i am trying to do.
I am getting invalid floading point on a sqrt. Basically all i am
doing is finding the distance froma point using pythagoras theorem but
for some reason i am getting negative valuse for my sqr(a) + sqr(b)

this is how it looks

M :Array[1..4ish] of longint (i tried real, but it may be bigger)
tx = 34;
tx = 159;
loopa = 1;
loopb = 1;
loopc = 1;
m[loopc] := sqr(tx - loopa) + sqr(tz - loopb);

for some reason this gives a negative answer so this doesnt work.
m[loopc] := sqrt(m[loopC]);

can any body tell me why or how to fix it?

cheers Nick Alcorso



Wed, 18 Jun 1902 08:00:00 GMT  
 Weird maths (sqrt)



Quote:
> I am getting stupid results in a procedure that i am trying to do.
> I am getting invalid floading point on a sqrt. Basically all i am
> doing is finding the distance froma point using pythagoras theorem but
> for some reason i am getting negative valuse for my sqr(a) + sqr(b)

> this is how it looks

> M :Array[1..4ish] of longint (i tried real, but it may be bigger)

        Huh? The range and precision of a real is greater than
        a longint.
Quote:
> tx = 34;
> tx = 159;
> loopa = 1;
> loopb = 1;
> loopc = 1;
> m[loopc] := sqr(tx - loopa) + sqr(tz - loopb);

        Furthermore assigning a real to a longint without rounding
        it is illegal.
Quote:

> for some reason this gives a negative answer so this doesnt work.
> m[loopc] := sqrt(m[loopC]);
> can any body tell me why or how to fix it?

        Declare your  array real.

Quote:
> cheers Nick Alcorso

--
Hilton Evans
***********************************************************
The two most important things in life are personal
health and time ... H.Evans 1995
**********************************************************
Chempen+ Chemical Structure Drawing
Software for Windows
http://www.ici.net/cust_pages/hfevans/chempen.htm


Wed, 18 Jun 1902 08:00:00 GMT  
 Weird maths (sqrt)

what is m:array[1..4ish] of longint ?

could you send the full program please?
(btw, I try your program, seem ok to me



: I am getting stupid results in a procedure that i am trying to do.
: I am getting invalid floading point on a sqrt. Basically all i am
: doing is finding the distance froma point using pythagoras theorem but
: for some reason i am getting negative valuse for my sqr(a) + sqr(b)
:
: this is how it looks
:
: M :Array[1..4ish] of longint (i tried real, but it may be bigger)
: tx = 34;
: tx = 159;
: loopa = 1;
: loopb = 1;
: loopc = 1;
: m[loopc] := sqr(tx - loopa) + sqr(tz - loopb);
:
: for some reason this gives a negative answer so this doesnt work.
: m[loopc] := sqrt(m[loopC]);
:
: can any body tell me why or how to fix it?
:
: cheers Nick Alcorso
:



Wed, 18 Jun 1902 08:00:00 GMT  
 Weird maths (sqrt)





: > I am getting stupid results in a procedure that i am trying to do.
: > I am getting invalid floading point on a sqrt. Basically all i am
: > doing is finding the distance froma point using pythagoras theorem but
: > for some reason i am getting negative valuse for my sqr(a) + sqr(b)
: >
: > this is how it looks
: >
: > M :Array[1..4ish] of longint (i tried real, but it may be bigger)
:       Huh? The range and precision of a real is greater than
:       a longint.
: > tx = 34;
: > tx = 159;
: > loopa = 1;
: > loopb = 1;
: > loopc = 1;
: > m[loopc] := sqr(tx - loopa) + sqr(tz - loopb);
:       Furthermore assigning a real to a longint without rounding
:       it is illegal.
: >
: > for some reason this gives a negative answer so this doesnt work.
: > m[loopc] := sqrt(m[loopC]);
: > can any body tell me why or how to fix it?
: >

I don't know what you did, but mine works

Program Calculate_Sqr;

Var

TX,TZ   :       Integer;
M       :       Array[1..4] of longint {Don't know for why}

Begin

tx      := 34;
tz      := 159;
loopa   := 1;
loopb   := 1;
loopc   := 1;
M[loopc] := Sqr(Longint(tx-loopa)) + Sqr(Longint(tz-loopa));

Writeln(M[loopc]);

end.



Wed, 18 Jun 1902 08:00:00 GMT  
 Weird maths (sqrt)

Quote:
>Subject: Weird maths (sqrt)

>Date: Sun, 28 Sep 1997 06:04:51 GMT

>I am getting stupid results in a procedure that i am trying to do.
>I am getting invalid floading point on a sqrt. Basically all i am
>doing is finding the distance froma point using pythagoras theorem but
>for some reason i am getting negative valuse for my sqr(a) + sqr(b)

>this is how it looks

>M :Array[1..4ish] of longint (i tried real, but it may be bigger)
><snip>

Your code is somewhat messed up but that's besides the point.  The real
reason, which nobody who had posted follow-ups for this so far knew, has to
do with the way sqr() works.  Specifically, _ the value the sqr function
returns is the ***same type as the parameter***_.  This is important
because it leads to potential arithmetic overflows.

For example, let's say X is of type INTEGER, which stores numbers *within
the range -32768..32767*.  Let's say X has the value 200.  Then SQR (X)
should be equal to 40,000.  However, this value will be stored to X's type,
the INTEGER type.  This is a problem though, since it's not within the
range of -32768..32767 that the INTEGER type supports.

So what does TP do now?  Basically it's going to do the following: 32768
become -32768, 32769 become -32767, 32770, becomes -32766, etc.  So under
this system 40,000 is stored as the INTEGER -25536!!!  And that's why
you're getting negative values!  It's pretty much TP's fault.  And it
doesn't matter if you store SQR (X) as a type with a bigger range, since
it's the type of *X* that counts.

The solution?  There are basically two ways.  One is to declare X to be a
type of larger range.  For example, if in the above example we declare X as
the type LONGINT, which supports a range of about -2 billion to +2 billion,
then SQR (X) will give the correct value of 40000, since that value is
within the range of the LONGINT.

Sometimes, however, we might not want to increase the range of the number
just so that SQR (X) will be evaluted correctly.  One might choose a type
with smaller range for speed or storage reasons.  Fortunately, in TP [not
standard Pascal] there is the second solution:  value typecasting.  For
example, in the example above, all we want is for SQR (X) to be treated as
a LONGINT value, not an INTEGER value.  To do so we can do the following:
SQR (LONGINT (X)).  What that does is that we're telling TP to treat the X
as a type of LONGINT even though it's really an INTEGER.  So now instead of
using the INTEGER type for the result, it'll treat the result as of type
LONGINT, and so you'll get the desired answer of 40,000!!!

OK, so how does all these apply to your program? Well, without a full
program it's hard for me to say, but a good place to start is to typecast
the entire expression in the SQR () function first to LONGINT, and if that
doesn't work, try REAL.  If things still don't work out then typecast the
individual values and variables.  Finally, make sure that you're assigning
the result to a type with an appropriate range.  For example, if X is of
type INTEGER,
X := SQR (LONGINT (X)) will still *not* work, since although SQR (LONGINT
(X)) gives the correct value of 40,000, you can't store that number in the
variable X, since its type is INTEGER.  It'll be like trying to stuff a
schoolbus load of people into a car.  It simply won't work.

I hope this helps.   Good luck!!!



Wed, 18 Jun 1902 08:00:00 GMT  
 Weird maths (sqrt)

Quote:
>I am getting stupid results in a procedure that i am trying to do.
>I am getting invalid floading point on a sqrt. Basically all i am
>doing is finding the distance froma point using pythagoras theorem but
>for some reason i am getting negative valuse for my sqr(a) + sqr(b)

>this is how it looks

>M :Array[1..4ish] of longint (i tried real, but it may be bigger)
>tx = 34;
>tx = 159;
>loopa = 1;
>loopb = 1;
>loopc = 1;
>m[loopc] := sqr(tx - loopa) + sqr(tz - loopb);

>for some reason this gives a negative answer so this doesnt work.
>m[loopc] := sqrt(m[loopC]);

>can any body tell me why or how to fix it?

>cheers Nick Alcorso

As far as i can imagine this error could be due to an overflow in the
calculation.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. HELP!!! Sqrt problem

2. Fast Sqrt

3. Vanishing SQRT-sign on graphic screen

4. I need Integer based Sqrt function, please.

5. Fast SQR, SQRT, Cos and sin (help!)

6. need a more powerful Sqrt function

7. Maths-Distribution

8. Programming For Maths

9. Floating Point Maths under BPW

10. MATHS FUNCTION PARSER

11. A few maths questions(groan).

12. Catching maths errors (eg divide by zero)

 

 
Powered by phpBB® Forum Software