Scala Tutorial Scala References

Scala - Math scalb() Method



The Scala Math scalb() method returns a x 2b. In special cases it returns the following:

  • If the first argument is NaN, NaN is returned.
  • If the first argument is infinite, then an infinity of the same sign is returned.
  • If the first argument is zero, then a zero is returned.

Syntax

def scalb(a: Double, b: Int): Double = 
                 java.lang.Math.scalb(a, b)
def scalb(a: Float, b: Int): Float = 
                 java.lang.Math.scalb(a, b)

Parameters

a Specify value to be scaled by power of 2.
b Specify power of 2 used to scale a.

Return Value

Returns a x 2b.

Exception

NA.

Example:

In the example below, Math.scalb() method returns a x 2b.

object MainObject {
  def main(args: Array[String]) {
    println(s"Math.scalb(2, 10) = ${Math.scalb(2, 10)}");  
    println(s"Math.scalb(3, 5) = ${Math.scalb(3, 5)}"); 
    println(s"Math.scalb(0, 5) = ${Math.scalb(0, 5)}");
    println(s"Math.scalb(Double.NaN, 3) = ${Math.scalb(Double.NaN, 3)}");
    println("Math.scalb(Double.PositiveInfinity, 2) = "
            + Math.scalb(Double.PositiveInfinity, 2));          
  }
}

The output of the above code will be:

Math.scalb(2, 10) = 2048.0
Math.scalb(3, 5) = 96.0
Math.scalb(0, 5) = 0.0
Math.scalb(Double.NaN, 3) = NaN
Math.scalb(Double.PositiveInfinity, 2) = Infinity

❮ Scala - Math Methods