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:
- Copy byte per byte the double to an unsigned integer (or long in the case of Java)
- 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)
- Copy byte per byte the unsigned integer back to a double