Comparing doubles

Discussion in 'Plugin Development' started by Tster, Feb 20, 2012.

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

    Tster

    Can i just use the standard methods( <= etc)
     
  2. Offline

    Njol

    Yes, except for ==.
    Decimal values can't be stored correctly in a binary number, e.g. 0.1 is stored as something like 0.0999945325. Because of this you might not always get what you expect, but something similar.
    Thus is you want to test two doubles (a & b) for equality you should use if (Math.abs(a - b) < 1e-40) (1e-40 is a very small number)
     
  3. Offline

    Acrobot

    Njol
    Or Double.compare(a, b) == 0 ?
     
  4. Offline

    Njol

    No, because even -0 and 0 are different for this method:
     
  5. Offline

    dillyg10

    I always tell people this, but you should only use doubles when you have very long decimal datatypes, otherwise just use floats :)
    But, do what the first reply said to compare them :D
     
Thread Status:
Not open for further replies.

Share This Page