ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1348. Goat in the Garden 2

Why Crash (floating-point invalid operation) at test 19?
Posted by Olja 23 Oct 2006 19:44
var l1,l2,lp,x1,y1,x2,y2,l,x3,y3,a1,a2,b1,b2,c1,c2,xp,yp:real;
begin
readln(x2,y2,x3,y3,x1,y1,l);
l1:=sqrt(sqr(x1-x2)+sqr(y1-y2));
l2:=sqrt(sqr(x1-x3)+sqr(y1-y3));
if (l1<l) and (l2<l) then begin
writeln('0.00'); writeln('0.00'); end else
begin
   a1:=y3-y2;
   b1:=x2-x3;
   c1:=y2*x3-x2*y3;
   a2:=-b1;
   b2:=a1;
   c2:=-(a2*x1+b2*y1);
   if (b1<>0) and (b2<>0) then
   begin
      xp:=(b2*c1-b1*c2)/(b1*a2-b2*a1);
      yp:=(-c1-a1*xp)/b1;
   end;
   if (b2=0) then
   begin
      yp:=-c1/b1;
      xp:=-c2/a2;
   end;
   if (b1=0) then
   begin
      xp:=-c1/a1;
      yp:=-c2/b2;
   end;
   if (((xp>=x2) and (xp<=x3)) or ((xp<=x2) and (xp>=x3))) and
      (((yp>=y2) and (yp<=y3)) or ((yp<=y2) and (yp>=y3)))then
      lp:=sqrt(sqr(x1-xp)+sqr(y1-yp))
      else if l1<=l2 then lp:=l1 else lp:=l2;
   if lp>=l then writeln(lp-l:0:2)
        else writeln('0.00');
   if l1<=l2 then
      if l2>=l then writeln(l2-l:0:2,' ')
        else writeln('0.00');
   if l2<l1 then
      if l1>=l then writeln(l1-l:0:2,' ')
        else writeln('0.00');
end;
end.

Please, help me...
Re: Why Crash (floating-point invalid operation) at test 19?
Posted by UdSU: Ajtkulov, Kotegov, Saitov 24 Oct 2006 01:26
Maybe real numbers comparison doesn't work well.
Try to change
a = b  for  abs(a - b) < EPS
a < b  for  a < b - EPS
a <= b  for  a - EPS < b,
where EPS is a rather little positive number like 1e-10,
for all real numders a and b in your program.

Also make sure you never divide by zero.