Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: why Float.floatToIntBits(2f) fails?

Tags:

scala

scala> Float.floatToI

On pressing tab here, it displays Float.floatToIntBits. But,

scala> Float.floatToIntBits(2f)
<console>:6: error: value floatToIntBits is not a member of object Float
       Float.floatToIntBits(2f)
             ^
like image 268
Marimuthu Madasamy Avatar asked Jan 22 '23 13:01

Marimuthu Madasamy


2 Answers

Float.floatToIntBits tries to call method on the object scala.runtime.Float (I think).

scala> Float
res2: Float.type = object scala.Float

You need java.lang.Float.floatToIntBits:

scala> java.lang.Float.floatToIntBits(2f)
res1: Int = 1073741824
like image 85
Alexey Romanov Avatar answered Jan 29 '23 17:01

Alexey Romanov


The REPL code-completion shows methods from all the Float objects available on the path (i.e.scala.Float scala.runtime.Float and java.lang.Float). However scala.Float scala.runtime.Float takes precedence over java.lang.Float and hence the error.

The following works:

scala> import java.lang.{Float => JFloat}
import java.lang.{Float=>JFloat}

scala> JFloat.floatToIntBits(2f)
res5: Int = 1073741824
like image 33
missingfaktor Avatar answered Jan 29 '23 17:01

missingfaktor