posted 1 year ago

I am trying to write a class that will calculate a player's level based off their total amount of experience points. For an example I have listed a sample of the experience chart below.

Level Total Experience Needed

1 0

2 300

3 600

4 1200

5 2400

6 4800

... ...

The formula to calculate the total amount of experience needed reach a level can be found as Experience = 300 * (2^(Level-1)). I realize that this formula does not work for Level 1 and will have code in place to handle that separately. However, since I want to find the level based off the current amount of experience rather than vice versa, I need to use the equation Level=Logbase2(Experience/300)-1. Java does not have a Logbase2 function from what I've read, so I am using the following code instead.

However, whenever I try to run this program I am given the answer -infinity. I am told that this has something to do with needing to use a double inside the Math.log() argument, however casting inside Math.log() does not seem to work.

Can someone please help me figure out how to get Math.log() to calculate properly? I tried reading other threads with questions about this (for example: http://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers) but the answers were confusing, especially the one that told me that "I had to add epsilon which is between 1e-11 and 1e-14". I am writing this program as part of an attempt to learn Java, so the theory behind why code works as well as the actual proper syntax would be greatly appreciated. Thank you for taking the time to help me with this, I hope you have a wonderful day.

Level Total Experience Needed

1 0

2 300

3 600

4 1200

5 2400

6 4800

... ...

The formula to calculate the total amount of experience needed reach a level can be found as Experience = 300 * (2^(Level-1)). I realize that this formula does not work for Level 1 and will have code in place to handle that separately. However, since I want to find the level based off the current amount of experience rather than vice versa, I need to use the equation Level=Logbase2(Experience/300)-1. Java does not have a Logbase2 function from what I've read, so I am using the following code instead.

However, whenever I try to run this program I am given the answer -infinity. I am told that this has something to do with needing to use a double inside the Math.log() argument, however casting inside Math.log() does not seem to work.

Can someone please help me figure out how to get Math.log() to calculate properly? I tried reading other threads with questions about this (for example: http://stackoverflow.com/questions/3305059/how-do-you-calculate-log-base-2-in-java-for-integers) but the answers were confusing, especially the one that told me that "I had to add epsilon which is between 1e-11 and 1e-14". I am writing this program as part of an attempt to learn Java, so the theory behind why code works as well as the actual proper syntax would be greatly appreciated. Thank you for taking the time to help me with this, I hope you have a wonderful day.

posted 1 year ago

Strange. I get a compiler error with the posted code:

error: incompatible types: possible lossy conversion from double to int

so I can execute it for testing.

whenever I try to run this program

Strange. I get a compiler error with the posted code:

error: incompatible types: possible lossy conversion from double to int

so I can execute it for testing.

posted 1 year ago

Welcome to the Ranch!

Campbell's hint was a bit subtle, maybe too subtle for you to catch. Consider the difference between the result of integer division and the result of floating point division.

Campbell's hint was a bit subtle, maybe too subtle for you to catch. Consider the difference between the result of integer division and the result of floating point division.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

Yassan Hikasa

Greenhorn

Posts: 6

posted 1 year ago

This code should compile I think. Not sure what happened, but I'm no longer getting −∞.

Still not entirely sure this gets me what I want, but that's probably a math problem, not a coding problem.

Norman Radder wrote:

whenever I try to run this program

Strange. I get a compiler error with the posted code:

error: incompatible types: possible lossy conversion from double to int

so I can execute it for testing.

This code should compile I think. Not sure what happened, but I'm no longer getting −∞.

Still not entirely sure this gets me what I want, but that's probably a math problem, not a coding problem.

Yassan Hikasa

Greenhorn

Posts: 6

Yassan Hikasa

Greenhorn

Posts: 6

posted 1 year ago

I guess I could use recursion, just trying to learn how java.lang.Math works for the OCA exam. Also, using logarithms seems like it would take 1 line, whereas using a loop would take several. Would a switch statement be better in ways other than being easier math-wise?

Piet Souris wrote:Still not quite right, I'm afraid. But must you use logarithms for some reason? That will make for a quite complicated formula.

If not, then how about using a simple recursion?

Like

I guess I could use recursion, just trying to learn how java.lang.Math works for the OCA exam. Also, using logarithms seems like it would take 1 line, whereas using a loop would take several. Would a switch statement be better in ways other than being easier math-wise?

posted 1 year ago

I don't think in OCA you could find something related to Math class. OCA is more about concepts, compier behaviour, rather than particular methods, well, there are some things about particular methods too, but I don't think they are from Math class. Regarding all that double check topics OCA exam covers.Yassan Hikasa wrote:just trying to learn how java.lang.Math works for the OCA exam.

Piet Souris

Rancher

Posts: 2454

80

posted 1 year ago

I would most certainly not use a swich, since you don;t know how high the exxperience can be.

Now, we have this scheme:

Now, it is not rocket science to derive the correct log2-formula, taking care of that '-infinity' situation, but as far I as can tell, all you need to know of the maths for OCAJP is the fact that (int) -.50 = 0.

But I like my little recursion much more, since it is very elegant. But that is up to you, of course.

Now, we have this scheme:

experiece | level | exp / 300 | (int)(log2(e / 300)) |
---|---|---|---|

0 | 1 | 0 | -inf |

300 | 2 | 1 | 0 |

600 | 3 | 2 | 1 |

900 | 3 | 3 | 1 |

1200 | 4 | 4 | 2 |

1500 | 4 | 5 | 2 |

1800 | 4 | 6 | 2 |

2100 | 4 | 7 | 2 |

2400 | 5 | 8 | 3 |

2700 | 5 | 9 | 3 |

et cetera |

Now, it is not rocket science to derive the correct log2-formula, taking care of that '-infinity' situation, but as far I as can tell, all you need to know of the maths for OCAJP is the fact that (int) -.50 = 0.

But I like my little recursion much more, since it is very elegant. But that is up to you, of course.