My Ray sphere intersection test:

t1:= Infinity;
t2:= Infinity;
DistX:= RayOrigin[X] - Sphere[X];
DistY:= RayOrigin[Y] - Sphere[Y];
DistZ:= RayOrigin[Z] - Sphere[Z];
b:= RayDirection[X]*DistX + RayDirection[Y]*DistY + RayDirection[Z]*DistZ;
c:= DistX*DistX + DistY*DistY + DistZ*DistZ - Radius*Radius;
d:= b * b - c;
if d >= 0.0 then
begin
sq:= sqrt(d);
t1:= -b - sq;
t2:= -b + sq;
end;
t1 - distance to the 1st hit, t2 - the 2nd. Infinity is a no hit constant. RayDirection must be normalized.