Well, if you want to be really weird like that you can apply the tangent half-angle and do something incredibly stupid like
ang = atan2(y2-y1,x2-x1)
d = ( (x2-x1)^2 + (y2-y1)^2 )^0.5
tan(ang/2) = (1 - cos(ang)) / sin(ang)
ang = atan2(y2-y1,x2-x1) = 2 * atan( (d - (x2-x1)) / (y2-y1) )
d = (y2-y1) * tan( atan2(y2-y1,x2-x1) / 2 ) + (x2-x1)
But the problem is definitely with your other code, not the math code itself. (Speaking of which someone double-check my math I haven't done calc in ages)
However, d = (|(x2-x1)/cos(ang)|) doesn't make much sense. ang is (basically) the angle of o2 to o1 so cos(ang) is essentially just the +- x-distance between them, i.e. x2-x1. d looks like it should always return 1.