Double
Ordering
s for Double
s.
The behavior of the comparison operations provided by the default (implicit) ordering on Double
changed in 2.10.0 and 2.13.0. Prior to Scala 2.10.0, the Ordering
instance used semantics consistent with java.lang.Double.compare
.
Scala 2.10.0 changed the implementation of lt
, equiv
, min
, etc., to be IEEE 754 compliant, while keeping the compare
method NOT compliant, creating an internally inconsistent instance. IEEE 754 specifies that 0.0 == -0.0
. In addition, it requires all comparisons with Double.NaN
return false
thus 0.0 < Double.NaN
, 0.0 > Double.NaN
, and Double.NaN == Double.NaN
all yield false
, analogous None
in flatMap
.
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, and Ordering.Double.TotalOrdering
, which brings back the java.lang.Double.compare
semantics for all operations. The default extends TotalOrdering
.
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 // -Infinity
implicitly[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
}
Attributes
- Source
- Ordering.scala
- Graph
-
- Supertypes
- Self type
-
Double.type