+ /* short-circuit obvious comparisons */
+ if (xvec_length == 0 || yvec_length == 0)
+ return (xvec_length == 0 && yvec_length == 0 ? 1.0 : 0.0);
+
+ if (lower_bound > 0)
+ {
+ /* Compute a quick upper bound.
+ Each edit is an insertion or deletion of an element, hence modifies
+ the length of the sequence by at most 1.
+ Therefore, when starting from a sequence X and ending at a sequence Y,
+ with N edits, | yvec_length - xvec_length | <= N. (Proof by
+ induction over N.)
+ So, at the end, we will have
+ xvec_edit_count + yvec_edit_count >= | xvec_length - yvec_length |.
+ and hence
+ result
+ = (xvec_length + yvec_length - (xvec_edit_count + yvec_edit_count))
+ / (xvec_length + yvec_length)
+ <= (xvec_length + yvec_length - | yvec_length - xvec_length |)
+ / (xvec_length + yvec_length)
+ = 2 * min (xvec_length, yvec_length) / (xvec_length + yvec_length).
+ */
+ volatile double upper_bound =
+ (double) (2 * MIN (xvec_length, yvec_length))
+ / (xvec_length + yvec_length);
+
+ if (upper_bound < lower_bound)
+ /* Return an arbitrary value < LOWER_BOUND. */
+ return 0.0;
+ }
+