/* Problem 2--Ladders
   Although the alley distance can be computed directly, it involves
   solving an eighth-degree equation.  This could be done, but it is
   MUCH easier find the height of the intersection point given the alley
   distance than the other way around.  So, with some trial-and-error
   akin to a binary search, this is what is done in this sample.
   If y is the height of the intersection point and l1 and l2 are the
   two ladder lengths, y MUST BE less than l1*l2/(l1+l2); this is how
   we check the mismeasured case. */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (int argc, char **argv);
double Process (double l1, double l2, double y);
double Compute (double l1, double l2, double x);

int main (int argc, char **argv) {

  FILE *in, *out;
  double l1, l2, y;
  int cs;

 in = fopen ("prob2.in","r");
 out = fopen ("prob2.out","w");
 cs = 0; /* Get input */
 while (fscanf (in,"%lf %lf %lf",&l1,&l2,&y),l1>0)
  if (y >= l1*l2/(l1+l2)) /* Check for bad case */
   fprintf (out,"Case %d: Barry mismeasured.\n",++cs);
  else fprintf (out,"Case %d: The alley is %.3f feet across.\n",++cs,
                     Process (l1,l2,y));
 fclose (in);
 fclose (out);
 return EXIT_SUCCESS;
}

/* Process returns the alley width by simply trying a bunch of alley
   widths and computing the height of the intersection point until
   we find a height that matches the given height. */
double Process (double l1, double l2, double y) {

  double x0, x1, x, yc;

 x0 = 0; x1 = l1 < l2 ? l1 : l2;
 while (x1-x0 > 1e-9) { /* akin to a binary search */
  x = (x0+x1)/2;
  yc = Compute (l1,l2,x);
  *(yc < y ? &x1 : &x0) = x;
 }
 return (x0+x1)/2;
}

/* Compute returns an intersection height given an alley width.  It
   turns out to be a very easy formula. */
double Compute (double l1, double l2, double x) {

  double a, b;

 a = sqrt (pow(l1,2)-pow(x,2));
 b = sqrt (pow(l2,2)-pow(x,2));
 return a*b/(a+b);
}
