scala.math.signum(-0d)

884 views
Skip to first unread message

Dominik Gruntz

unread,
May 7, 2012, 8:39:42 PM5/7/12
to scala-i...@googlegroups.com
Is there a reason that scala.math.signum is implemented in scala

  def signum(x: Double): Double =
    if (x == 0d) 0d
    else if (x < 0) -1.0
    else if (x > 0) 1.0
    else x // NaN

and not delegated to the Java implementation

  def signum(x: Double): Double = java.lang.Math.signum(x)

The behavior is different on negative zero (-0d). The above Scala implementation returns 0d whereas the Java implementation returns -0d (realized that when I looked at SI-5766).
The Scala code could easily be fixed by writing
  def signum(x: Double): Double =
    if (x == 0d) x
    else if (x < 0) -1.0
    else if (x > 0) 1.0
    else x // NaN


Btw: ijuma wrote in https://github.com/scala/scala/commit/771f5ca5a11541866b57481ce0068fe8511c320d that
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.


This difference to the Java behavior should either be eliminated or documented.
Dominik



Paul Phillips

unread,
May 7, 2012, 8:48:04 PM5/7/12
to scala-i...@googlegroups.com


On Mon, May 7, 2012 at 5:39 PM, Dominik Gruntz <dominik...@gmail.com> wrote:
Is there a reason that scala.math.signum is implemented in scala

When it was added we (I use "we" loosely as I wasn't here yet) still supported java 4, which lacks the function. That's the only reason.

Ismael Juma

unread,
May 7, 2012, 9:28:12 PM5/7/12
to scala-i...@googlegroups.com
On Tue, May 8, 2012 at 1:39 AM, Dominik Gruntz <dominik...@gmail.com> wrote:
Btw: ijuma wrote in https://github.com/scala/scala/commit/771f5ca5a11541866b57481ce0068fe8511c320d that
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.


This difference to the Java behavior should either be eliminated or documented.
Personally, I think the difference should be eliminated and the Scala methods should simply call the Java ones. The reason why I didn't include that change in my pull request is because I wanted it to be uncontroversial.

Best,
Ismael

Dominik Gruntz

unread,
May 8, 2012, 3:17:52 AM5/8/12
to scala-i...@googlegroups.com
you are right, it is not available under http://docs.oracle.com/javase/1.4.2/docs/api/.
I agree with Ismael, that the behavior should be equivalent to Java's one (and the behavior of the Java implementation makes sense).
I will change it and make a pull request.
Dominik

martin odersky

unread,
May 8, 2012, 3:22:40 AM5/8/12
to scala-i...@googlegroups.com
I vote for "eliminated". - Martin 



--
Martin Odersky
Prof., EPFL and Chairman, Typesafe
PSED, 1015 Lausanne, Switzerland
Tel. EPFL: +41 21 693 6863
Tel. Typesafe: +41 21 691 4967

Reply all
Reply to author
Forward
0 new messages