Skip to main content
Became Hot Network Question
spelling
Source Link

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 finfine 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.

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 fin 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.

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.

Source Link

In what range does incrementing and decrementing Java doubles work?

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 fin 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.