object Float
Ordering
s for Float
s.
The behavior of the comparison operations provided by the default (implicit)
ordering on Float
changed in 2.10.0 and 2.13.0.
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
.
Recognizing the limitation of the IEEE 754 semantics in terms of ordering,
Scala 2.13.0 created two instances: Ordering.Float.IeeeOrdering
, which retains
the IEEE 754 semantics from Scala 2.12.x, and Ordering.Float.TotalOrdering
,
which brings back the java.lang.Float.compare
semantics for all operations.
The default extends TotalOrdering
.
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 }
- Source
- Ordering.scala
- Alphabetic
- By Inheritance
- Float
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- trait IeeeOrdering extends Ordering[Float]
An ordering for
Float
s which is consistent with IEEE specifications whenever possible.An ordering for
Float
s which is consistent with IEEE specifications whenever possible.lt
,lteq
,equiv
,gteq
andgt
are consistent with primitive comparison operations forFloat
s, 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 as java.lang.Float.compare.
Because the behavior of
Float
s 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 in scala.math.This ordering may be preferable for numeric contexts.
- See also
- trait TotalOrdering extends Ordering[Float]
An ordering for
Float
s which is a fully consistent total ordering, and treatsNaN
as larger than all otherFloat
values; it behaves the same as java.lang.Float.compare.An ordering for
Float
s which is a fully consistent total ordering, and treatsNaN
as larger than all otherFloat
values; it behaves the same as java.lang.Float.compare.Because the behavior of
Float
s 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 in scala.math.This ordering may be preferable for sorting collections.
- See also
Value Members
- implicit object IeeeOrdering extends IeeeOrdering
- implicit object TotalOrdering extends TotalOrdering
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int
,Float
,Array
orOption
which are accessible in all Scala compilation units without explicit qualification or imports.Notable packages include:
scala.collection
and its sub-packages contain Scala's collections frameworkscala.collection.immutable
- Immutable, sequential data-structures such asVector
,List
,Range
,HashMap
orHashSet
scala.collection.mutable
- Mutable, sequential data-structures such asArrayBuffer
,StringBuilder
,HashMap
orHashSet
scala.collection.concurrent
- Mutable, concurrent data-structures such asTrieMap
scala.concurrent
- Primitives for concurrent programming such asFutures
andPromises
scala.io
- Input and output operationsscala.math
- Basic math functions and additional numeric types likeBigInt
andBigDecimal
scala.sys
- Interaction with other processes and the operating systemscala.util.matching
- Regular expressionsOther packages exist. See the complete list on the right.
Additional parts of the standard library are shipped as separate libraries. These include:
scala.reflect
- Scala's reflection API (scala-reflect.jar)scala.xml
- XML parsing, manipulation, and serialization (scala-xml.jar)scala.collection.parallel
- Parallel collections (scala-parallel-collections.jar)scala.util.parsing
- Parser combinators (scala-parser-combinators.jar)scala.swing
- A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)Automatic imports
Identifiers in the scala package and the
scala.Predef
object are always in scope by default.Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example,
List
is an alias forscala.collection.immutable.List
.Other aliases refer to classes provided by the underlying platform. For example, on the JVM,
String
is an alias forjava.lang.String
.