Skip to content

Commit

Permalink
[squash] add deprecated orderings
Browse files Browse the repository at this point in the history
  • Loading branch information
NthPortal committed May 27, 2018
1 parent aeedef1 commit 42d121e
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/library/scala/math/Numeric.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ object Numeric {
// logic in Numeric base trait mishandles abs(-0.0f)
override def abs(x: Float): Float = math.abs(x)
}
implicit object FloatIsFractional extends FloatIsFractional with Ordering.FloatIeeeOrdering
implicit object FloatIsFractional extends FloatIsFractional with Ordering.Float.IeeeOrdering

trait DoubleIsFractional extends Fractional[Double] {
def plus(x: Double, y: Double): Double = x + y
Expand All @@ -158,7 +158,7 @@ object Numeric {
// logic in Numeric base trait mishandles abs(-0.0)
override def abs(x: Double): Double = math.abs(x)
}
implicit object DoubleIsFractional extends DoubleIsFractional with Ordering.DoubleIeeeOrdering
implicit object DoubleIsFractional extends DoubleIsFractional with Ordering.Double.IeeeOrdering

trait BigDecimalIsConflicted extends Numeric[BigDecimal] {
def plus(x: BigDecimal, y: BigDecimal): BigDecimal = x + y
Expand Down
217 changes: 111 additions & 106 deletions src/library/scala/math/Ordering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,6 @@ trait LowPriorityOrderingImplicits {
*
* It contains many implicit orderings as well as well as methods to construct
* new orderings.
*
* @define doubleOrdering Because the behaviour of `Double`s specified by IEEE is
* not consistent with a total ordering when dealing with
* `NaN`, there are two orderings defined for `Double`:
* `DoubleTotalOrdering`, which is consistent with a total
* ordering, and `DoubleIeeeOrdering`, which is consistent
* as much as possible with IEEE spec and floating point
* operations defined in [[scala.math]].
* @define floatOrdering Because the behaviour of `Float`s specified by IEEE is
* not consistent with a total ordering when dealing with
* `NaN`, there are two orderings defined for `Float`:
* `FloatTotalOrdering`, which is consistent with a total
* ordering, and `FloatIeeeOrdering`, which is consistent
* as much as possible with IEEE spec and floating point
* operations defined in [[scala.math]].
*/
object Ordering extends LowPriorityOrderingImplicits {
@inline def apply[T](implicit ord: Ordering[T]) = ord
Expand Down Expand Up @@ -329,105 +314,125 @@ object Ordering extends LowPriorityOrderingImplicits {
}
implicit object Long extends LongOrdering

/** An ordering for `Float`s 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()]].
*
* $floatOrdering
*
* This ordering may be preferable for sorting collections.
/** `Ordering`s for `Float`s.
*
* @see [[FloatIeeeOrdering]]
* @define floatOrdering Because the behaviour of `Float`s 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]].
*/
trait FloatTotalOrdering extends Ordering[Float] {
def compare(x: Float, y: Float) = java.lang.Float.compare(x, y)
}
@implicitAmbiguous("There are multiple ways to order Floats (Ordering.FloatTotalOrdering, " +
"Ordering.FloatIeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.")
implicit object FloatTotalOrdering extends FloatTotalOrdering
object Float {
/** An ordering for `Float`s 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()]].
*
* $floatOrdering
*
* This ordering may be preferable for sorting collections.
*
* @see [[IeeeOrdering]]
*/
trait TotalOrdering extends Ordering[Float] {
def compare(x: Float, y: Float) = java.lang.Float.compare(x, y)
}
implicit object TotalOrdering extends TotalOrdering

/** An ordering for `Float`s which is consistent with IEEE specifications
* whenever possible.
*
* - `lt`, `lteq`, `equiv`, `gteq` and `gt` are consistent with primitive
* comparison operations for `Float`s, 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()]].
*
* $floatOrdering
*
* This ordering may be preferable for numeric contexts.
*
* @see [[FloatTotalOrdering]]
*/
trait FloatIeeeOrdering extends Ordering[Float] {
def compare(x: Float, y: Float) = java.lang.Float.compare(x, y)

override def lteq(x: Float, y: Float): Boolean = x <= y
override def gteq(x: Float, y: Float): Boolean = x >= y
override def lt(x: Float, y: Float): Boolean = x < y
override def gt(x: Float, y: Float): Boolean = x > y
override def equiv(x: Float, y: Float): Boolean = x == y
override def max(x: Float, y: Float): Float = math.max(x, y)
override def min(x: Float, y: Float): Float = math.min(x, y)
}
@implicitAmbiguous("There are multiple ways to order Floats (Ordering.FloatTotalOrdering, " +
"Ordering.FloatIeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.")
implicit object FloatIeeeOrdering extends FloatIeeeOrdering

/** An ordering for `Double`s which is a fully consistent total ordering,
* and treats `NaN` as larger than all other `Double` values; it behaves
* the same as [[java.lang.Double.compare()]].
*
* $doubleOrdering
*
* This ordering may be preferable for sorting collections.
*
* @see [[DoubleIeeeOrdering]]
*/
trait DoubleTotalOrdering extends Ordering[Double] {
def compare(x: Double, y: Double) = java.lang.Double.compare(x, y)
/** An ordering for `Float`s which is consistent with IEEE specifications
* whenever possible.
*
* - `lt`, `lteq`, `equiv`, `gteq` and `gt` are consistent with primitive
* comparison operations for `Float`s, 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()]].
*
* $floatOrdering
*
* This ordering may be preferable for numeric contexts.
*
* @see [[TotalOrdering]]
*/
trait IeeeOrdering extends Ordering[Float] {
def compare(x: Float, y: Float) = java.lang.Float.compare(x, y)

override def lteq(x: Float, y: Float): Boolean = x <= y
override def gteq(x: Float, y: Float): Boolean = x >= y
override def lt(x: Float, y: Float): Boolean = x < y
override def gt(x: Float, y: Float): Boolean = x > y
override def equiv(x: Float, y: Float): Boolean = x == y
override def max(x: Float, y: Float): Float = math.max(x, y)
override def min(x: Float, y: Float): Float = math.min(x, y)
}
implicit object IeeeOrdering extends IeeeOrdering
}
@implicitAmbiguous("There are multiple ways to order Doubles (Ordering.DoubleTotalOrdering, " +
"Ordering.DoubleIeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.")
implicit object DoubleTotalOrdering extends DoubleTotalOrdering
@deprecated("There are multiple ways to order Floats (Ordering.Float.TotalOrdering, " +
"Ordering.Float.IeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.", since = "2.13.0")
implicit object DeprecatedFloatOrdering extends Float.TotalOrdering

/** An ordering for `Double`s which is consistent with IEEE specifications
* whenever possible.
/** `Ordering`s for `Double`s.
*
* - `lt`, `lteq`, `equiv`, `gteq` and `gt` are consistent with primitive
* comparison operations for `Double`s, 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.Double.compare()]].
*
* $doubleOrdering
*
* This ordering may be preferable for numeric contexts.
*
* @see [[DoubleTotalOrdering]]
* @define doubleOrdering Because the behaviour of `Double`s specified by IEEE is
* not consistent with a total ordering when dealing with
* `NaN`, there are two orderings defined for `Double`:
* `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]].
*/
trait DoubleIeeeOrdering extends Ordering[Double] {
def compare(x: Double, y: Double) = java.lang.Double.compare(x, y)

override def lteq(x: Double, y: Double): Boolean = x <= y
override def gteq(x: Double, y: Double): Boolean = x >= y
override def lt(x: Double, y: Double): Boolean = x < y
override def gt(x: Double, y: Double): Boolean = x > y
override def equiv(x: Double, y: Double): Boolean = x == y
override def max(x: Double, y: Double): Double = math.max(x, y)
override def min(x: Double, y: Double): Double = math.min(x, y)
object Double {
/** An ordering for `Double`s which is a fully consistent total ordering,
* and treats `NaN` as larger than all other `Double` values; it behaves
* the same as [[java.lang.Double.compare()]].
*
* $doubleOrdering
*
* This ordering may be preferable for sorting collections.
*
* @see [[IeeeOrdering]]
*/
trait TotalOrdering extends Ordering[Double] {
def compare(x: Double, y: Double) = java.lang.Double.compare(x, y)
}
implicit object TotalOrdering extends TotalOrdering

/** An ordering for `Double`s which is consistent with IEEE specifications
* whenever possible.
*
* - `lt`, `lteq`, `equiv`, `gteq` and `gt` are consistent with primitive
* comparison operations for `Double`s, 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.Double.compare()]].
*
* $doubleOrdering
*
* This ordering may be preferable for numeric contexts.
*
* @see [[TotalOrdering]]
*/
trait IeeeOrdering extends Ordering[Double] {
def compare(x: Double, y: Double) = java.lang.Double.compare(x, y)

override def lteq(x: Double, y: Double): Boolean = x <= y
override def gteq(x: Double, y: Double): Boolean = x >= y
override def lt(x: Double, y: Double): Boolean = x < y
override def gt(x: Double, y: Double): Boolean = x > y
override def equiv(x: Double, y: Double): Boolean = x == y
override def max(x: Double, y: Double): Double = math.max(x, y)
override def min(x: Double, y: Double): Double = math.min(x, y)
}
implicit object IeeeOrdering extends IeeeOrdering
}
@implicitAmbiguous("There are multiple ways to order Doubles (Ordering.DoubleTotalOrdering, " +
"Ordering.DoubleIeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.")
implicit object DoubleIeeeOrdering extends DoubleIeeeOrdering
@deprecated("There are multiple ways to order Doubles (Ordering.Double.TotalOrdering, " +
"Ordering.Double.IeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it " +
"explicitly. See the documentation for details.", since = "2.13.0")
implicit object DeprecatedDoubleOrdering extends Double.TotalOrdering

trait BigIntOrdering extends Ordering[BigInt] {
def compare(x: BigInt, y: BigInt) = x.compare(y)
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/runtime/RichDouble.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package runtime

final class RichDouble(val self: Double) extends AnyVal with FractionalProxy[Double] {
protected def num: Fractional[Double] = scala.math.Numeric.DoubleIsFractional
protected def ord: Ordering[Double] = scala.math.Ordering.DoubleTotalOrdering
protected def ord: Ordering[Double] = scala.math.Ordering.Double.TotalOrdering

override def doubleValue() = self
override def floatValue() = self.toFloat
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/runtime/RichFloat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package runtime

final class RichFloat(val self: Float) extends AnyVal with FractionalProxy[Float] {
protected def num: Fractional[Float] = scala.math.Numeric.FloatIsFractional
protected def ord: Ordering[Float] = scala.math.Ordering.FloatTotalOrdering
protected def ord: Ordering[Float] = scala.math.Ordering.Float.TotalOrdering

override def doubleValue() = self.toDouble
override def floatValue() = self
Expand Down
10 changes: 0 additions & 10 deletions test/files/neg/t10511.check

This file was deleted.

9 changes: 9 additions & 0 deletions test/files/pos/t10511.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
t10511.scala:2: warning: object DeprecatedFloatOrdering in object Ordering is deprecated (since 2.13.0): There are multiple ways to order Floats (Ordering.Float.TotalOrdering, Ordering.Float.IeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it explicitly. See the documentation for details.
val f = Ordering[Float]
^
t10511.scala:3: warning: object DeprecatedDoubleOrdering in object Ordering is deprecated (since 2.13.0): There are multiple ways to order Doubles (Ordering.Double.TotalOrdering, Ordering.Double.IeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it explicitly. See the documentation for details.
val d = Ordering[Double]
^
t10511.scala:6: warning: object DeprecatedDoubleOrdering in object Ordering is deprecated (since 2.13.0): There are multiple ways to order Doubles (Ordering.Double.TotalOrdering, Ordering.Double.IeeeOrdering). Specify one by using a local import, assigning an implicit val, or passing it explicitly. See the documentation for details.
list.sorted
^
1 change: 1 addition & 0 deletions test/files/pos/t10511.flags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-deprecation
File renamed without changes.
2 changes: 1 addition & 1 deletion test/files/run/OrderingTest.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.math.Ordering.DoubleTotalOrdering
import scala.math.Ordering.Double.TotalOrdering

object Test extends App {
def test[T](t1 : T, t2 : T)(implicit ord : Ordering[T]) = {
Expand Down
2 changes: 1 addition & 1 deletion test/files/run/t5857.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import scala.math.Ordering.DoubleIeeeOrdering
import scala.math.Ordering.Double.IeeeOrdering


object Test {
Expand Down
3 changes: 2 additions & 1 deletion test/junit/scala/math/OrderingTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

import scala.math.Ordering.{FloatTotalOrdering, DoubleTotalOrdering}
import scala.math.Ordering.Float.TotalOrdering
import scala.math.Ordering.Double.TotalOrdering

import java.{lang => jl}

Expand Down

0 comments on commit 42d121e

Please sign in to comment.