Scala Tutorial Scala References

Scala - Math nextDown() Method



The Scala Math nextDown() method returns the floating-point value adjacent to argument in the direction of negative infinity. In special cases it returns the following:

  • If either argument is a NaN, then NaN is returned.
  • If the argument is negative infinity, the result is negative infinity.
  • If the argument is zero, the result is -Double.MinValue or -Float.MinValue.

Syntax

def nextDown(x: Double): Double = java.lang.Math.nextDown(x)
def nextDown(x: Float): Float = java.lang.Math.nextDown(x)

Parameters

x Specify starting floating-point value.

Return Value

Returns the floating-point value adjacent to argument in the direction of negative infinity.

Exception

NA.

Example:

In the example below, Math.nextDown() method returns the floating-point value adjacent to argument in the direction of negative infinity.

object MainObject {
  def main(args: Array[String]) {
    println("Math.nextDown(10.55) = "
            + Math.nextDown(10.55)); 
    println("Math.nextDown(-10.55) = "
            + Math.nextDown(-10.55)); 
    println("Math.nextDown(0) = "
            + Math.nextDown(0));     
    println("Math.nextDown(Double.NaN) = "
            + Math.nextDown(Double.NaN));             
    println("Math.nextDown(Double.PositiveInfinity) = "
            + Math.nextDown(Double.PositiveInfinity));  
  }
}

The output of the above code will be:

Math.nextDown(10.55) = 10.549999999999999
Math.nextDown(-10.55) = -10.550000000000002
Math.nextDown(0) = -1.4E-45
Math.nextDown(Double.NaN) = NaN
Math.nextDown(Double.PositiveInfinity) = 1.7976931348623157E308

❮ Scala - Math Methods

5