Let’s start with this method:
public static int levelToExp(int level) {
return (int) Math.floor(1000 * Math.pow(level, 1.5));
}
This formula takes the number level
, raises to 1.5, multiplies by 1000 and rounds.
The inverse would be, ignoring the rounding at the end, divide by 1000 and raise to the inverse of 1.5.
What is the inverse of 1.5? Well, considering that 1.5 = 3/2, then the inverse is 2/3.
So the formula would be something like thereby:
Math.pow(exp / 1000.0, 2 / 3.0);
Why like? Why we still have to deal with rounding and the errors it introduces. For example:
class TesteExp {
public static void main(String[] args) {
int level = 2;
int exp = (int) Math.floor(1000 * Math.pow(level, 1.5));
System.out.println(exp);
double level2 = Math.pow(exp / 1000.0, 2 / 3.0);
System.out.println(level2);
System.out.println(Math.floor(level2));
}
}
Here’s the way out (see here working on ideone):
2828
1.9997986463957098
1.0
That is, to reach level 2, it is necessary to have 2828 EXP points. But by reversing the equation, we arrive at level 1.9997986463957098, which if we round it down would be level 1, not 2. This is because the required XP would be a number between 2828 and 2829, but the Math.floor
the rounded down.
If instead of rounding the level down, we were rounding it up, it wouldn’t work either because in case the amount of EXP is 2827, it would be at level 2, instead of level 1.
So we don’t know yet whether we should round up or down. What can we do? Kick both shapes and see which of the corresponding levels fits:
public static int expToLevel(int exp) {
double level = Math.pow(exp / 1000.0, 2 / 3.0);
int a = (int) Math.floor(level);
int b = (int) Math.ceil(level);
return levelToExp(b) > exp ? a : b;
}
Let’s test this whole code:
public static void main(String[] args) {
for (int i = 0; i <= 20; i++) {
int exp = levelToExp(i);
System.out.print("Para o level " + i + " precisamos de " + exp + " EXP. ");
int provaReal = expToLevel(exp);
int antes = expToLevel(exp - 1);
int depois = expToLevel(exp + 1);
System.out.print((exp - 1) + " EXP = Level " + antes + ". ");
System.out.print(exp + " EXP = Level " + provaReal + ". ");
System.out.print((exp + 1) + " EXP = Level " + depois + ".");
System.out.println();
}
}
Here’s the way out (See here working on ideone):
Para o level 0 precisamos de 0 EXP. -1 EXP = Level 0. 0 EXP = Level 0. 1 EXP = Level 0.
Para o level 1 precisamos de 1000 EXP. 999 EXP = Level 0. 1000 EXP = Level 1. 1001 EXP = Level 1.
Para o level 2 precisamos de 2828 EXP. 2827 EXP = Level 1. 2828 EXP = Level 2. 2829 EXP = Level 2.
Para o level 3 precisamos de 5196 EXP. 5195 EXP = Level 2. 5196 EXP = Level 3. 5197 EXP = Level 3.
Para o level 4 precisamos de 8000 EXP. 7999 EXP = Level 3. 8000 EXP = Level 4. 8001 EXP = Level 4.
Para o level 5 precisamos de 11180 EXP. 11179 EXP = Level 4. 11180 EXP = Level 5. 11181 EXP = Level 5.
Para o level 6 precisamos de 14696 EXP. 14695 EXP = Level 5. 14696 EXP = Level 6. 14697 EXP = Level 6.
Para o level 7 precisamos de 18520 EXP. 18519 EXP = Level 6. 18520 EXP = Level 7. 18521 EXP = Level 7.
Para o level 8 precisamos de 22627 EXP. 22626 EXP = Level 7. 22627 EXP = Level 8. 22628 EXP = Level 8.
Para o level 9 precisamos de 27000 EXP. 26999 EXP = Level 8. 27000 EXP = Level 9. 27001 EXP = Level 9.
Para o level 10 precisamos de 31622 EXP. 31621 EXP = Level 9. 31622 EXP = Level 10. 31623 EXP = Level 10.
Para o level 11 precisamos de 36482 EXP. 36481 EXP = Level 10. 36482 EXP = Level 11. 36483 EXP = Level 11.
Para o level 12 precisamos de 41569 EXP. 41568 EXP = Level 11. 41569 EXP = Level 12. 41570 EXP = Level 12.
Para o level 13 precisamos de 46872 EXP. 46871 EXP = Level 12. 46872 EXP = Level 13. 46873 EXP = Level 13.
Para o level 14 precisamos de 52383 EXP. 52382 EXP = Level 13. 52383 EXP = Level 14. 52384 EXP = Level 14.
Para o level 15 precisamos de 58094 EXP. 58093 EXP = Level 14. 58094 EXP = Level 15. 58095 EXP = Level 15.
Para o level 16 precisamos de 64000 EXP. 63999 EXP = Level 15. 64000 EXP = Level 16. 64001 EXP = Level 16.
Para o level 17 precisamos de 70092 EXP. 70091 EXP = Level 16. 70092 EXP = Level 17. 70093 EXP = Level 17.
Para o level 18 precisamos de 76367 EXP. 76366 EXP = Level 17. 76367 EXP = Level 18. 76368 EXP = Level 18.
Para o level 19 precisamos de 82819 EXP. 82818 EXP = Level 18. 82819 EXP = Level 19. 82820 EXP = Level 19.
Para o level 20 precisamos de 89442 EXP. 89441 EXP = Level 19. 89442 EXP = Level 20. 89443 EXP = Level 20.
Note that the output matches the expected. With one point less EXP, it drops to the previous level. With one more XP point, it remains at the same level.
I looked and I didn’t find it, but I swear I remember this one exact Ask around. Maybe it wasn’t you who posted it, but anyway, I didn’t find it anymore. Anyway you already had your answer (very good, to the point that you saved your question by the details of rounding - otherwise the question would only be a question of mathematics, and I would say that would be outside the scope of the site). :)
– Luiz Vieira