Skip to content

Commit 26c87af

Browse files
committed
avoid boxing
scala.runtime.Rich{Double, Float} has `isNaN` and these are value class. Also java.lang.{Double, Float} has `isNaN`. - https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#isNaN-- - https://docs.oracle.com/javase/8/docs/api/java/lang/Float.html#isNaN-- We can't call `RichDouble#isNaN` because `implicit def double2Double(x: Double): java.lang.Double` is higher priority than `implicit def doubleWrapper(x: Double): RichDouble` ``` $ scala -version Scala code runner version 2.11.8 -- Copyright 2002-2016, LAMP/EPFL $ scala -Xprint:jvm -e "1.0.isNaN" [[syntax trees at end of jvm]] // scalacmd616162202928036892.scala package <empty> { object Main extends Object { def main(args: Array[String]): Unit = { new <$anon: Object>(); () }; def <init>(): Main.type = { Main.super.<init>(); () } }; final class anon$1 extends Object { def <init>(): <$anon: Object> = { anon$1.super.<init>(); scala.this.Predef.double2Double(1.0).isNaN(); () } } } ```
1 parent 4a487ca commit 26c87af

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/library/scala/concurrent/duration/Duration.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ object Duration {
120120
def fromNanos(nanos: Double): Duration = {
121121
if (nanos.isInfinite)
122122
if (nanos > 0) Inf else MinusInf
123-
else if (nanos.isNaN)
123+
else if (JDouble.isNaN(nanos))
124124
Undefined
125125
else if (nanos > Long.MaxValue || nanos < Long.MinValue)
126126
throw new IllegalArgumentException("trying to construct too large duration with " + nanos + "ns")
@@ -196,11 +196,11 @@ object Duration {
196196
}
197197

198198
def *(factor: Double): Duration =
199-
if (factor == 0d || factor.isNaN) Undefined
199+
if (factor == 0d || JDouble.isNaN(factor)) Undefined
200200
else if (factor < 0d) -this
201201
else this
202202
def /(divisor: Double): Duration =
203-
if (divisor.isNaN || divisor.isInfinite) Undefined
203+
if (JDouble.isNaN(divisor) || divisor.isInfinite) Undefined
204204
else if ((divisor compare 0d) < 0) -this
205205
else this
206206
def /(divisor: Duration): Double = divisor match {
@@ -627,13 +627,13 @@ final class FiniteDuration(val length: Long, val unit: TimeUnit) extends Duratio
627627

628628
def *(factor: Double) =
629629
if (!factor.isInfinite) fromNanos(toNanos * factor)
630-
else if (factor.isNaN) Undefined
630+
else if (JDouble.isNaN(factor)) Undefined
631631
else if ((factor > 0) ^ (this < Zero)) Inf
632632
else MinusInf
633633

634634
def /(divisor: Double) =
635635
if (!divisor.isInfinite) fromNanos(toNanos / divisor)
636-
else if (divisor.isNaN) Undefined
636+
else if (JDouble.isNaN(divisor)) Undefined
637637
else Zero
638638

639639
// if this is made a constant, then scalac will elide the conditional and always return +0.0, SI-6331

src/reflect/scala/reflect/internal/Constants.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ trait Constants extends api.Constants {
8787
}
8888

8989
def isNaN = value match {
90-
case f: Float => f.isNaN
91-
case d: Double => d.isNaN
90+
case f: Float => java.lang.Float.isNaN(f)
91+
case d: Double => java.lang.Double.isNaN(d)
9292
case _ => false
9393
}
9494

0 commit comments

Comments
 (0)