Home > other >  Method of Implementing Java Math.nextAfter() for JDK 5
Method of Implementing Java Math.nextAfter() for JDK 5

Time:12-15

I need to use JDK1.5 for a particular application and have noticed that the Math library does not contain the Math.nextAfter() function. I can probably kludge a version of this but I am wondering if anyone has a clever method of getting the next Double value in a manner that emulates the Math.nextAfter() function.

CodePudding user response:

You can find the implementation of the function made by OpenJDK here (be careful of their license, you may or may not be able to use their code depending on the licence you have in your project).
From what I see what their code does is the following:

  1. Copy byte per byte the double to an unsigned integer (or long in the case of Java)
  2. Do an unsigned increment(or decrement depending on the direction) to the unsigned integer (in Java you have to consider the negative and positive values separately, so 1 if the value is positive or -1 if the value is negative, this is because of how 2's complement work)
  3. Copy byte per byte the unsigned integer back to a double
  • Related