Skip to content

compare -0 and 0 as equal in the float and double holders - #174

Open
aizu-m wants to merge 2 commits into
apache:trunkfrom
aizu-m:signed-zero-float-compare
Open

aizu-m wants to merge 2 commits into
apache:trunkfrom
aizu-m:signed-zero-float-compare

Conversation

@aizu-m

@aizu-m aizu-m commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

[xs:float] minInclusive 0, value -0
XMLBeans = INVALID: cvc-minInclusive-valid
Xerces = VALID

[xs:float] unique 0 / -0
   XMLBeans = VALID
   Xerces   = INVALID: cvc-identity-constraint.4.1: Duplicate unique value [0.0E1]

Found these in a differential run of XmlObject.validate() against the JDK Xerces validator over xs:float and xs:double: 14 disagreements, every one with -0 on a side.

They all come from JavaFloatHolder.compare, which drops through to a floatToIntBits tie-break once neither < nor > holds. floatToIntBits(-0.0f) is 0x80000000, so -0 sorts below 0 and never compares equal to it. JavaDoubleHolder.compare does the same with doubleToLongBits. The tie-break is there for NaN, which has to equal itself although NaN == NaN is false in Java; signed zero reaches it by accident. XML Schema Part 2 1.0 2E 3.2.4 is explicit about it: "This datatype differs from that of [IEEE 754-1985] in that there is only one NaN and only one zero." 3.2.5 says the same for double. 1.1 3.3.4 and 3.3.5 keep both zeros in the value space but define equality as identity "except that 0 = -0", and add that "0 and -0 are thus equivalent for purposes of enumerations and identity constraints, as well as for minimum and maximum values". Either edition gives the same answer here.

Four observations. xs:unique and xs:key miss a duplicate pair 0 / -0, because IdentityConstraint keys field values on valueEquals and valueHashCode. xs:keyref rejects a reference written -0 against a key written 0. minInclusive='0' rejects -0 and maxInclusive='-0' rejects 0, while minExclusive='-0' accepts 0, and enumeration value='0' does not match -0. XmlObject.valueEquals, compareValue and valueHashCode disagree with the value space.

Both compare methods now return 0 when the two values are IEEE-equal, ahead of the bit tie-break, and value_hash_code maps -0 onto 0 so valueEquals and valueHashCode stay in step. NaN keeps the tie-break, so it still equals itself and nothing else. Every other value behaves as before, and all 14 cases agree with Xerces after the change. Regression test added.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two non-blocking notes:

  • XmlObject.valueEquals, compareValue and valueHashCode change for 0 / -0, so this deserves a line in the release notes.
  • I am not sure about the spec citation. From memory the wording on signed zero differs between the XML Schema 1.0 editions and 1.1 (I have not rechecked it), so it may be worth confirming the reference. Agreement with Xerces is the stronger argument for the change in any case.

@aizu-m

aizu-m commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Good instinct on the citation, I'd quoted the value space line and that's the weak form of the argument. The direct sentence is in 1.0 2E 3.2.4, repeated in 3.2.5 for double: "This datatype differs from that of [IEEE 754-1985] in that there is only one NaN and only one zero." I've updated the description to that and the code comments now name the sections.

The wording does differ in 1.1, as you remembered. 3.3.4 and 3.3.5 put positiveZero and negativeZero in the value space as separate values, then define equality as identity "except that 0 = -0", and spell out the consequence: "0 and -0 are thus equivalent for purposes of enumerations and identity constraints, as well as for minimum and maximum values". So 1.0 has one zero and 1.1 has two equal zeros, and both land on the same result for this patch.

Worth flagging that the other half of that 1.1 change runs the opposite way: 1.1 makes NaN != NaN, where 1.0 has NaN equal to itself. XMLBeans follows 1.0 there and this patch deliberately leaves it be, so the bit tie-break stays for NaN.

On the release notes, CHANGES.txt stops at 5.0.1 and points at status.html, so there's nothing in the tree for me to edit. Line if it's useful:

  • xs:float and xs:double now compare 0 and -0 as equal, per XML Schema Part 2 3.2.4 and 3.2.5. This changes XmlObject.valueEquals, compareValue and valueHashCode for those two types, and the results of enumeration, identity constraints and the four bounding facets. NaN behaviour is unchanged.

Glad to put that wherever the notes actually live if you'd rather it came in with the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants