Skip to content

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

Open
aizu-m wants to merge 1 commit into
apache:trunkfrom
aizu-m:signed-zero-float-compare
Open

aizu-m wants to merge 1 commit 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 3.2.4 gives float the value space m x 2^e plus the three special values, so there is one zero and -0 and 0 are the same value.

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.

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