Abyssal
2009-06-28 06:27:48
{*******************************************************************************
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
By: Abyssal
Description: Returns the third side of the triangle.
*******************************************************************************}
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if(NeedHypot)then
Result := Sqrt((Sqr(a) + Sqr(bHypot)));
if(not(NeedHypot))then
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;
Set boolean "NeedHypot" if the hypothesis is the variable being solved for. Example:
Pythag(3, 4, True);
If you have the hypothesis and are solving for a different side (doesn't matter which), "NeedHypot" becomes false, and the hypotenuse is put into "b", hence the weird naming scheme. Example:
Pythag(4, 5, False);
Test it!
program PythagTest;
function Pythag(a, bHypot: Extended; NeedHypot: Boolean): Extended;
begin
if(NeedHypot)then
Result := Sqrt((Sqr(a) + Sqr(bHypot)));
if(not(NeedHypot))then
Result := Sqrt((Sqr(bHypot) - Sqr(a)));
end;
begin
Writeln(Pythag(4, 5, False));
Writeln(Pythag(3, 4, True));
end.