Solved [Java] Get when a double value is equal to a whole number

Discussion in 'Plugin Development' started by sirrus86, Jan 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    sirrus86

    This feels like a really stupid question here, and I'm sure I'm overlooking something, but I can't seem to get this to work the way I need it to.

    Within a scheduled task I have a for-loop that increments a double value from 0.0 to, as an example, 30.0, incrementing by 0.1 each iteration. I need to know when that value becomes a whole number, such as 1.0, 2.0, etc. Below is my script, what I've tried, and the results:
    Code:
    Bukkit.getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
        @Override
        public void run() {
            for (double d = 0.0D; d < 30.0D; d += 0.1D) {
                if (d % 1.0D == 0.0D) { // Use modulus to determine if whole number
                    System.out.println("d = " + d);
                }
            }
        }
    }, 0L, 1L);
    However when running this I get:
    Code:
    d = 0.0
    d = 19.0
    d = 0.0
    d = 19.0
    This repeats, never deviating.
    I've already tried substituting the last conditional with these others:
    Code:
    if (d == (int) d) // See if double value is equal to integer value
    
    if (Double.toString(d).endsWith(".0")) // See if the double literally ends in ".0"
    
    if (Math.IEEEremainder(d, 1.0D) == 0.0D) // Use the IEEE method of determining remainder
    All of these methods produce the same results: d = 0.0 and d = 19.0

    My only thought is that, despite telling it to increment by 0.1, it may be incrementing by a different amount (like 0.099999 or something), preventing it from being equal to whole numbers consistently... in which case I'd wonder what the next best solution would be.

    My main stipulation is that the 30.0D part will end up being a configurable double value that the user can change, so I'd prefer not to change the for-statement to use integers.
     
    Last edited: Jan 21, 2015
  2. Offline

    Skionz

    @sirrus86 Don't increment doubles. Increment integers then multiply them by a double.
     
  3. Offline

    1Rogue

    You used an assignment operator, not a comparison operator.

    At any rate, just compare with an int:

    Code:java
    1. if (d % 1 == 0) { //...


    Your real error is probably from some floating point error. Try printing the value every time.
     
  4. Offline

    sirrus86

    @Skionz Yeah starting to think I might have to...

    @1Rogue Sorry that was a typo. Comparing to an integer produced the same result.

    Edit: Just ran a test to see what the double values were with each iteration, turns out it sometimes increments by 0.1 and sometimes by 0.099999, so... Guess this won't work.

    Thanks for your help guys.
     
  5. Offline

    1Rogue

     
  6. Offline

    sirrus86

    Yeah I just saw this after my last post, my bad.
     
Thread Status:
Not open for further replies.

Share This Page