Orderings forDoubles.
The behavior of the comparison operations provided by the default (implicit) ordering onDouble changed in 2.10.0 and 2.13.0. Prior to Scala 2.10.0, theOrdering instance used semantics consistent withjava.lang.Double.compare.
Scala 2.10.0 changed the implementation oflt,equiv,min, etc., to be IEEE 754 compliant, while keeping thecompare method NOT compliant, creating an internally inconsistent instance. IEEE 754 specifies that0.0 == -0.0. In addition, it requires all comparisons withDouble.NaN returnfalse thus0.0 < Double.NaN,0.0 > Double.NaN, andDouble.NaN == Double.NaN all yieldfalse, analogousNone inflatMap.
Recognizing the limitation of the IEEE 754 semantics in terms of ordering, Scala 2.13.0 created two instances:Ordering.Double.IeeeOrdering, which retains the IEEE 754 semantics from Scala 2.12.x, andOrdering.Double.TotalOrdering, which brings back thejava.lang.Double.compare semantics for all operations. The default extendsTotalOrdering.
List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).sorted // List(-Infinity, 0.0, 1.0, NaN)List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).min // -Infinityimplicitly[Ordering[Double]].lt(0.0, 0.0 / 0.0) // true{ import Ordering.Double.IeeeOrdering List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).sorted // List(-Infinity, 0.0, 1.0, NaN) List(0.0, 1.0, 0.0 / 0.0, -1.0 / 0.0).min // NaN implicitly[Ordering[Double]].lt(0.0, 0.0 / 0.0) // false}An ordering forDoubles which is consistent with IEEE specifications whenever possible.
An ordering forDoubles which is consistent with IEEE specifications whenever possible.
lt,lteq,equiv,gteq andgt are consistent with primitive comparison operations forDoubles, and returnfalse when called withNaN.
min andmax are consistent withmath.min andmath.max, and returnNaN when called withNaN as either argument.
compare behaves the same asjava.lang.Double.compare.
Because the behavior ofDoubles specified by IEEE is not consistent with a total ordering when dealing withNaN, there are two orderings defined forDouble:TotalOrdering, which is consistent with a total ordering, andIeeeOrdering, which is consistent as much as possible with IEEE spec and floating point operations defined inscala.math.
This ordering may be preferable for numeric contexts.
An ordering forDoubles which is a fully consistent total ordering, and treatsNaN as larger than all otherDouble values; it behaves the same asjava.lang.Double.compare.
An ordering forDoubles which is a fully consistent total ordering, and treatsNaN as larger than all otherDouble values; it behaves the same asjava.lang.Double.compare.
Because the behavior ofDoubles specified by IEEE is not consistent with a total ordering when dealing withNaN, there are two orderings defined forDouble:TotalOrdering, which is consistent with a total ordering, andIeeeOrdering, which is consistent as much as possible with IEEE spec and floating point operations defined inscala.math.
This ordering may be preferable for sorting collections.