• Post Reply Bookmark Topic Watch Topic
  • New Topic

Log Base 2 and Casting  RSS feed

 
Yassan Hikasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you ever get that to compile, consider what 304 / 300 evaluates to. Also consider which number will produce a logarithm of −∞.
You can only solve that sort of problem with pencil and paper, not by coding.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yassan Hikasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check your math:

Is this really the inverse:

Looks like an incorrect sign.

 
Yassan Hikasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Check your math:

Is this really the inverse:

Looks like an incorrect sign.






Did I do something wrong?
 
Yassan Hikasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yassan Hikasa wrote:just trying to learn how java.lang.Math works for the OCA exam.
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.
 
Piet Souris
Master Rancher
Posts: 2041
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would most certainly not use a swich, since you don;t know how high the exxperience can be.

Now, we have this scheme:
experiecelevelexp / 300(int)(log2(e / 300))
010-inf
300210
600321
900331
1200442
1500452
1800462
2100472
2400583
2700593
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!