Java - Math nextAfter(float x, double y) Method



Description

The Java Math nextAfter(float start, double direction) returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal a value equivalent to the second argument is returned. Special cases −

  • If either argument is a NaN, then NaN is returned.

  • If both arguments are signed zeros, a value equivalent to direction is returned.

  • If start is Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.

  • If start is infinite and direction has a value such that the result should have a smaller magnitude, Float.MAX_VALUE with the same sign as start is returned.

  • If start is equal to Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.

Declaration

Following is the declaration for java.lang.Math.nextAfter() method

public static float nextAfter(float start, double direction)

Parameters

  • start − starting floating-point value

  • direction − value indicating which of start's neighbors or start should be returned

Return Value

This method returns the floating-point number adjacent to start in the direction of direction.

Exception

NA

Getting Next Float After two Positive Float and Double Values Example

The following example shows the usage of Math nextAfter() method for two positive values.

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get two numbers
      float x = 98759.765f;
      double y = 154.28764;
   
      // print the next number for x towards y
      System.out.println("Math.nextAfter(" + x + "," + y + ")="
         + Math.nextAfter(x, y));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Math.nextAfter(98759.766,154.28764)=98759.76

Getting Next Float After a Positive Float and a Negative Double Values Example

The following example shows the usage of Math nextAfter() method for a positive and a negative value.

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get two numbers
      float x = -98759.765f;
      double y = 154.28764;
   
      // print the next number for x towards y
      System.out.println("Math.nextAfter(" + x + "," + y + ")="
         + Math.nextAfter(x, y));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Math.nextAfter(-98759.766,154.28764)=-98759.76

Getting Next Float After two Negative Float and Double Values Example

The following example shows the usage of Math nextAfter() method for negative values.

package com.tutorialspoint;

public class MathDemo {

   public static void main(String[] args) {

      // get two numbers
      float x = -98759.765f;
      double y = -154.28764;
   
      // print the next number for x towards y
      System.out.println("Math.nextAfter(" + x + "," + y + ")="
         + Math.nextAfter(x, y));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Math.nextAfter(-98759.766,-154.28764)=-98759.76
java_lang_math.htm
Advertisements