Board index » delphi » Problem using SQRT.

Problem using SQRT.

Hi,

I have a little problem using the sqrt function,which is I think easy to
solve,but I don't know how.

I have three reals X,Y,Z now I want to calculate:
Z:=sqrt(1-X*X-Y*Y)

It gives me EInvalidOp,invalid floating point operation. What's that?
PS I'm sure 1-X*X-Y*Y is greater than or equal to 0.

Thanks,Alex.

 

Re:Problem using SQRT.


Alex,

Don't forget the rules of maths! You have to look at how the calc is being
evaluated.
Mulitply and divides are calculated before subtraction.
e.g.
(1-X*X-Y*Y)
won't be the same as
((1-X)*(X-Y)*Y)
etc.

James

Quote
Alex Gout wrote in message <898540054.295...@news.molyvos.net>...
>Hi,

>I have a little problem using the sqrt function,which is I think easy to
>solve,but I don't know how.

>I have three reals X,Y,Z now I want to calculate:
>Z:=sqrt(1-X*X-Y*Y)

>It gives me EInvalidOp,invalid floating point operation. What's that?
>PS I'm sure 1-X*X-Y*Y is greater than or equal to 0.

>Thanks,Alex.

Re:Problem using SQRT.


In article <898540054.295...@news.molyvos.net>, "Alex Gout" <al...@molyvos.net>
writes:

Quote
>It gives me EInvalidOp,invalid floating point operation. What's that?
>PS I'm sure 1-X*X-Y*Y is greater than or equal to 0.

You've either entered an integer number for X and Y, or your result somewhere
in the middle of your caluculation has become negative.

Enter whole number values as number decimal zero. Use brackets to ensure
correct order of calculation and clarity of function. Do you mean to do :-

 (((1 - X) * X) - Y) * Y   ==  xy - x^2y - y^2
 . . . or . . .
 (1- X) * (X - Y) * Y  ==  xy - x^2y - y^2 - xy^2

Alan Lloyd
alangll...@aol.com

Re:Problem using SQRT.


Alex,

As the others have pointed out, the precedence of operations is
probably to blame here. Let's look at how Delphi would process
your calculation:

1-X*X-Y*Y

Because * has higher precedence, the calculation becomes

1 - (X*X) - (Y*Y)

Assuming that X = 2 and Y = 3, this becomes

1 - (2 * 2) - (3 * 3) = 1 - 4 - 9 = -13

HTH

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Quote
Alex Gout wrote:

> Hi,

> I have a little problem using the sqrt function,which is I think easy to
> solve,but I don't know how.

> I have three reals X,Y,Z now I want to calculate:
> Z:=sqrt(1-X*X-Y*Y)

> It gives me EInvalidOp,invalid floating point operation. What's that?
> PS I'm sure 1-X*X-Y*Y is greater than or equal to 0.

> Thanks,Alex.

Other Threads