I'm writing a Java class that tracks a fractional number, but needs to be able to increment and decrement it. Using a double
for this number seems to work fine for small values, but I'm trying to figure out its limits.
I was thinking that one way of determining it might be to run a program that incremented a double
and a long
until they were no longer equal. However this takes too long to run.
double x = 0;
long y = 0;
while ((long)x == y)
{
x++;
y++;
}
System.out.println("X: " + x);
System.out.println("Y: " + y);
What are double
's limits where you can no longer increment a value and actually add one to it? I'd think that decrementing a double
would have a similar problem with negative numbers at some point.
Math.nextUp()
and the one ofMath.ulp()