Context

An own XPath 2.0 / Schematron evaluator was compared against Saxon-HE 12.8 and validators built on it, running business rules over XML documents validated without a schema, so every value is untyped. Hundreds of hand-written cases agreed. About 1 in 15 randomised documents did not, always on rules that compare an amount with zero.

Insight

By the spec, in a general comparison an untyped value compared with a number is cast to xs:double, and -0 equals +0. Saxon-HE 12.8 orders such comparisons totally, as Java does: -0 sorts before +0, NaN above everything. Measured with 159 expressions:

(: nz is the untyped text "-0.00", pz the untyped text "0.00" :)
nz = 0e0 (: false in Saxon, true by the spec :)
pz = -0e0 (: false in Saxon :)
nz = 0 (: false: integer comparison only for integer text such as "-0" :)
xs:double('-0.00') = 0 (: true: typed values are not affected :)
round(sum(m) * 100) div 100 = pz (: false when the sum is a tiny negative, because the result is -0 :)

Two consequences:

  • A rule like “tax amount = 0” fires when a document writes -0.00, for example a credit note computed as -100 × 0 %.
  • A rounded total that comes out as -0 does not equal a written 0.00.

Takeaways:

  • Decide which one you follow. Strictly by the spec, your engine passes documents that the Saxon-based validator your counterpart runs will reject. Reproducing Saxon took about 40 lines, behind a switch, and gave zero deviations.
  • Keep a randomised oracle comparison as a permanent test. No hand-written case and no official example file contained a negative zero; only random data found it.

Docs

A docs check did not find this described on these pages (no claim beyond them):