Solved Double Decimal Decrementing

Discussion in 'Spigot Plugin Development' started by DarkKnights22, Jan 7, 2020.

  1. Code (Java):
    double count = 0.4;
            for (String hologram: repositoryCrate.getHologram()) {
                new ArmorStandCreator(hologram, block.getLocation().clone().add(0.5, count, 0.5));
                count--;
            }
    How would I make count go down in decimals of 0.1, so it would be 0.4, 0.3, 0.2, 0.1 etc.
     
  2. SteelPhoenix

    Moderator

    subtract 0.1 instead of 1?
     
    • Funny Funny x 2
  3. Yeah like SteelPhoenix said.
    count -= 0.1;
    You cant rly decrement a decimal number because of its composition (vz exp mantissa)
     
    • Like Like x 1
  4. num-- or num++ is basically the same as num-=1 and num+=1
     
  5. Not quite sure if this is relevant for java and if the compiler corrects this but in almost every CISC CPU there is a special “add” instruction that is named INC (or DEC) and that instruction increments (or decrements) by 1 the value of a register (or memory).
    If you use another instruction for addition you have to add both operands you want to sum.
    That makes INC/DEC more efficient:
    They take less space (no space for operands).
    In CISC they execute faster (because you save time to load operands).

    (in short "i++" might be faster than "i = i+1" or "i += 1")
    Also ++i is (apparently) also somehow faster than i++

    But then again. Not quite sure how this is done in the JVM.
    Also you should generally not think about stuff like that in a high level language like Java.

    PS ill write a jmh Benchmark...
    Result:
    Its all the same... except adding something else than 1 or adding a double.
     
    #5 7smile7, Jan 7, 2020
    Last edited: Jan 7, 2020
  6. Now here we have an optimization freak I like it :p
    But please do your benchmarks before you write something like this.
    While it is true what you say about CISC processors, please notice that we are normally talking about massively overturned things where something like "add" or "inc" really does not matter.
    First of all: Both of these commands have a performance duration of one clock cycle, provided that the operand is inside a Cache close to the processor. Now concerning your massive memory overhead of one operand:
    8 byte / 2GB - is a very small number.
    Also while it is true that a CISC processor would execute the task faster (again by 2 or 3 clock cycles) no modern processor is actually CISC. Even thou these processors accept CISC instructions, the internally decode them into way faster RISC instructions.

    Add that to the fact that the JVM is a virtual machine and if you get a faster result for any benchmark (ADD vs INC) this is due to the high amounts of dynamics (Caches, predictions, multicore) in today's processors.

    Don't want to discourage you, I like your style, but optimization at such a level is (even on assembly level) never necessary.
     
  7. /optimization off

    It's probably just opinion based in this case, but in general I try to avoid floating point counters (for example in loops) and I think it's a general rule that you should avoid that. Instead you can increment or decrement an integer and calculate the required floating point inside the loop. This is probably not an efficient solution but much cleaner. Example:
    Code (Java):
    for (int i = 0; i <= 10; i++) {
        double d = ((double) i) / 10.;
    }
    Further reading: https://stackoverflow.com/questions...s-loop-counters-and-their-fractional-incremen
     
    • Agree Agree x 1
  8. agreed. Tho he used a loop using an Iterator and decremented additionally
     
  9. Haha calm down, I know it could possibly be a one CPU instruction for i++/i-- but i was saying how it has the same outcome as the other code.
     
  10. I think OP was just looking for an infix notation of this. You can use: add(0.5, (count = count - 0.1), 0.5)) to have it all neat.
     
    • Agree Agree x 1