Orderings forFloats.
The default extendsOrdering.Float.TotalOrdering.
Ordering.Float.TotalOrdering uses thejava.lang.Float.compare semantics for all operations. Scala also provides theOrdering.Float.IeeeOrdering semantics. Which uses the IEEE 754 semantics for float ordering.
Historically:IeeeOrdering was used in Scala from 2.10.x through 2.12.x. This changed in 2.13.0 toTotalOrdering.
Prior to Scala 2.10.0, theOrdering instance used semantics consistent withjava.lang.Float.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.0F == -0.0F. In addition, it requires all comparisons withFloat.NaN returnfalse thus0.0F < Float.NaN,0.0F > Float.NaN, andFloat.NaN == Float.NaN all yieldfalse, analogousNone inflatMap.
List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).sorted // List(-Infinity, 0.0, 1.0, NaN)List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).min // -Infinityimplicitly[Ordering[Float]].lt(0.0F, 0.0F / 0.0F) // true{ import Ordering.Float.IeeeOrdering List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).sorted // List(-Infinity, 0.0, 1.0, NaN) List(0.0F, 1.0F, 0.0F / 0.0F, -1.0F / 0.0F).min // NaN implicitly[Ordering[Float]].lt(0.0F, 0.0F / 0.0F) // false}An ordering forFloats which is consistent with IEEE specifications whenever possible.
An ordering forFloats which is consistent with IEEE specifications whenever possible.
lt,lteq,equiv,gteq andgt are consistent with primitive comparison operations forFloats, 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.Float.compare.
Because the behavior ofFloats specified by IEEE is not consistent with a total ordering when dealing withNaN, there are two orderings defined forFloat: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 forFloats which is a fully consistent total ordering, and treatsNaN as larger than all otherFloat values; it behaves the same asjava.lang.Float.compare.
An ordering forFloats which is a fully consistent total ordering, and treatsNaN as larger than all otherFloat values; it behaves the same asjava.lang.Float.compare.
Because the behavior ofFloats specified by IEEE is not consistent with a total ordering when dealing withNaN, there are two orderings defined forFloat: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.