Skip to content

Commit

Permalink
Delegate to Java's implementation of signum for Long and Int.
Browse files Browse the repository at this point in the history
The Java implementation is faster as it doesn't have branches.

java.lang.Math includes implementations of signum for Double and Float,
but I didn't change the ones in scala.math because there is a difference
on how negative zero is handled.
  • Loading branch information
ijuma committed Dec 3, 2011
1 parent 2e14c12 commit 771f5ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/library/scala/math/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,9 @@ package object math {
else if (x > 0) 1.0f
else x // NaN

def signum(x: Long): Long =
if (x == 0l) 0l
else if (x < 0) -1l
else 1l

def signum(x: Int): Int =
if (x == 0) 0
else if (x < 0) -1
else 1
def signum(x: Long): Long = java.lang.Long.signum(x)

def signum(x: Int): Int = java.lang.Integer.signum(x)

// -----------------------------------------------------------------------
// root functions
Expand Down
15 changes: 15 additions & 0 deletions test/files/jvm/signum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
object Test {
def main(args: Array[String]) {
assert(math.signum(Long.MaxValue) == 1L)
assert(math.signum(1L) == 1L)
assert(math.signum(0L) == 0L)
assert(math.signum(-1L) == -1L)
assert(math.signum(Long.MinValue) == -1L)

assert(math.signum(Int.MaxValue) == 1)
assert(math.signum(1) == 1)
assert(math.signum(0) == 0)
assert(math.signum(-1) == -1)
assert(math.signum(Int.MinValue) == -1)
}
}

0 comments on commit 771f5ca

Please sign in to comment.