correctness of physics function



I wrote a function for a physics simulation to try to figure out the
force that an object bounces off a wall with assuming conservation of
kinetic energy. It seems to work, but I was wondering if anyone has
any ideas on how to prove its correctness.

It is shown below:

// determines the magnitude of the force that results in conservation
of energy
double solver(Vector v, Vector w, Vector f, Vector r)
{
f = normalize(f);
double startenergy = 0.5*sqr(mag(v)) + 0.5*sqr(mag(w));

double minmag=.00000001, curmag=5, maxmag=1000000;

for (int ctr = 0; ctr < 100; ctr++)
{

Vector newv = v+curmag*f;

Vector torque = crossprod(r, curmag*f);
Vector neww = cube.w+torque;

double curenergy = 0.5*sqr(mag(newv)) + 0.5*sqr(mag(neww));
double energyerror = curenergy - startenergy;
if (energyerror > 0) maxmag = curmag;
else if (energyerror < 0) minmag = curmag;
curmag = (minmag + maxmag)/2.0;
}

return curmag;
}

(BTW, I assume mass=1 and moment of inertia=1 and time of force=1.)

.