Float

scala.math.Ordering.Float
object Float

Orderings for Floats.

The default extends Ordering.Float.TotalOrdering.

Ordering.Float.TotalOrdering uses the java.lang.Float.compare semantics for all operations. Scala also provides the Ordering.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 to TotalOrdering.

Prior to Scala 2.10.0, the Ordering instance used semantics consistent with java.lang.Float.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.0F == -0.0F. In addition, it requires all comparisons with Float.NaN return false thus 0.0F < Float.NaN, 0.0F > Float.NaN, and Float.NaN == Float.NaN all yield false, analogous None in flatMap.

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         // -Infinity
implicitly[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
}

Attributes

Source
Ordering.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Float.type

Members list

Type members

Classlikes

object IeeeOrdering extends IeeeOrdering

Attributes

Companion
trait
Source
Ordering.scala
Supertypes
trait IeeeOrdering
trait Ordering[Float]
trait Equiv[Float]
trait Serializable
trait Comparator[Float]
class Object
trait Matchable
class Any
Show all
Self type
trait IeeeOrdering extends Ordering[Float]

An ordering for Floats which is consistent with IEEE specifications whenever possible.

An ordering for Floats which is consistent with IEEE specifications whenever possible.

  • lt, lteq, equiv, gteq and gt are consistent with primitive comparison operations for Floats, and return false when called with NaN.

  • min and max are consistent with math.min and math.max, and return NaN when called with NaN as either argument.

  • compare behaves the same as java.lang.Float.compare.

Because the behavior of Floats specified by IEEE is not consistent with a total ordering when dealing with NaN, there are two orderings defined for Float: TotalOrdering, which is consistent with a total ordering, and IeeeOrdering, which is consistent as much as possible with IEEE spec and floating point operations defined in scala.math.

This ordering may be preferable for numeric contexts.

Attributes

See also
Companion
object
Source
Ordering.scala
Supertypes
trait Ordering[Float]
trait Equiv[Float]
trait Serializable
trait Comparator[Float]
class Object
trait Matchable
class Any
Show all
Known subtypes
object TotalOrdering extends TotalOrdering

Attributes

Companion
trait
Source
Ordering.scala
Supertypes
trait Ordering[Float]
trait Equiv[Float]
trait Serializable
trait Comparator[Float]
class Object
trait Matchable
class Any
Show all
Self type
trait TotalOrdering extends Ordering[Float]

An ordering for Floats which is a fully consistent total ordering, and treats NaN as larger than all other Float values; it behaves the same as java.lang.Float.compare.

An ordering for Floats which is a fully consistent total ordering, and treats NaN as larger than all other Float values; it behaves the same as java.lang.Float.compare.

Because the behavior of Floats specified by IEEE is not consistent with a total ordering when dealing with NaN, there are two orderings defined for Float: TotalOrdering, which is consistent with a total ordering, and IeeeOrdering, which is consistent as much as possible with IEEE spec and floating point operations defined in scala.math.

This ordering may be preferable for sorting collections.

Attributes

See also
Companion
object
Source
Ordering.scala
Supertypes
trait Ordering[Float]
trait Equiv[Float]
trait Serializable
trait Comparator[Float]
class Object
trait Matchable
class Any
Show all
Known subtypes