Scala Tutorial Scala References

Scala - Math nextUp() Method



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

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

Syntax

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

Parameters

x Specify starting floating-point value.

Return Value

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

Exception

NA.

Example:

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

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

The output of the above code will be:

Math.nextUp(10.55) = 10.550000000000002
Math.nextUp(-10.55) = -10.549999999999999
Math.nextUp(0) = 1.4E-45
Math.nextUp(Double.NaN) = NaN
Math.nextUp(Double.PositiveInfinity) = Infinity

❮ Scala - Math Methods