Health

Discussion in 'Plugin Development' started by WolfMage1, Jul 8, 2016.

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

    WolfMage1

    Whenever I use player.getHealth() instead of returning 2.5 for example, it returns 2.43224353221, Really confused,
     
  2. @WolfMage1 Java doubles are never fun, just round it.
     
  3. Offline

    WolfMage1

    Is there any other way or is rounding just the best option for this?
     
  4. @WolfMage1 I think rounding would be the best, Java can have major issues when doing simple maths with doubles. It can return 4.9999999999 instead of 5 for example. I can't think of a better way unfortunately.
     
  5. Offline

    Zombie_Striker

    @WolfMage1
    In case you do not know how to round, use Math.round(double) to round that double to the nearest int. If you wish to keep some of the decimals, use the method provide at THIS link.
     
  6. Offline

    WolfMage1

    thats what I started using after bwfcwalshy suggested rounding
     
  7. If your issue has been resolved, please mark the thread as such :)
     
  8. Offline

    I Al Istannen

    @WolfMage1
    In case you want to know why and don't already know it:
    doubles and floats are floating point numbers. This means they are NOT saved in this way:
    "xxx.XXX", where "x" is a digit before the decimal point and "X" after it.
    This would limit the accuracy in both ways, as you would need more digits before and after the decimal point to display higher numbers. These fixed-point numbers are a lot more pleasant to deal with though, as there are NO rounding errors. They are only used where absolute precision is needed, as the computer is not optimized to deal with them, which means they are slower to work with.

    Floating points consist of 3 parts:
    1. The sign (+/-)
    2. The exponent (It is stored in some special way, called "Excess" representation. Just a way to store negative numbers. But I wanted to mention it, as I think the site at the end mentions it.)
    3. The mantissa (mantisse, dunno what is english :p)

    It is really similar to the scientific notation (e.g. 1.87*10^10).
    The exponent just means what power of two the mantissa is multiplied with (10^10 in the example above).
    The mantissa are the digits of the number ((1).87).

    The exponent is to base 2 normally, and there is the problem. Although there are some tricks (like denormalized numbers, when the exponent is 0), you can't get more precise that a sum of powers of two.

    If you have 1001 as mantissa, this means
    1 * 2^-1 + 0 * 2^-2 + 0* 2^-3 + 1 * 2^-4
    But this sum can only be so accurate. Some numbers, which can be represented by a sum of powers of two (up to a reasonable amount of mantissa bytes) won't cause any problems. Any power of two within the range shouldn't, for example.

    But other numbers may not be representable with this system, which causes these slight errors.

    Sorry if I made a mistake somewhere along, some time has passed since I looked at this ;) Please correct any errors you find.

    As always, wikipedia has some information (which is not understandable as always :p)
    Here is something else, but google will probably help you more.


    What I found quite helpful (and cool!) is this site. It allows you to flip all the bytes in a "float" (32 bit, which means single precision. "double" means double precision. Surprisingly :p) by yourself and see how the binary and the decimal output change.
     
    mine-care likes this.
  9. Offline

    MCKrypto14

    what the heck did you do to this simple question
     
  10. Offline

    I Al Istannen

    ArsenArsen likes this.
Thread Status:
Not open for further replies.

Share This Page