blabla123 Posted June 1, 2014 Posted June 1, 2014 public void onStart() { startTime = System.currentTimeMillis(); planksCreated = 0; } onLoop() { planksCreated += client.getInventory().getAmount(userChoiceP); } onPaint() { long planksPerHour = (planksCreated * 3600000) / (System.currentTimeMillis() - startTime); } Maybe I'm overlooking something, but why is this giving me correct results up till around 30 mins running the script (1500/hr) and after 30 mins it goes to (-1500/hr) and goes towards 0... ? Time counter is running ok, planks counter running ok, even trips per hour counter running ok, just this one went crazy. Only way '(planksCreated * 3600000) / (System.currentTimeMillis() - startTime)' would be giving negative result is when startTime > System.currentTimeMillis() but how is that possible?
Nitrousek Posted June 1, 2014 Posted June 1, 2014 public void onStart() { startTime = System.currentTimeMillis(); planksCreated = 0; } onLoop() { planksCreated += client.getInventory().getAmount(userChoiceP); } onPaint() { long planksPerHour = (planksCreated * 3600000) / (System.currentTimeMillis() - startTime); } Maybe I'm overlooking something, but why is this giving me correct results up till around 30 mins running the script (1500/hr) and after 30 mins it goes to (-1500/hr) and goes towards 0... ? Time counter is running ok, planks counter running ok, even trips per hour counter running ok, just this one went crazy. Only way '(planksCreated * 3600000) / (System.currentTimeMillis() - startTime)' would be giving negative result is when startTime > System.currentTimeMillis() but how is that possible? You have not supplied enough information. Also, I'm assuming that's not everything you have for determining planksCreated planksCreated += client.getInventory().getAmount(userChoiceP); You haven't even shown us what data types those all are.
blabla123 Posted June 1, 2014 Author Posted June 1, 2014 (edited) Declared at the beginning, outside any methods. int userChoiceP; long startTime; int planksCreated; And actually that line is all I use for counting planks created and it works fine everytime. As I said before, all other counters work fine. Now that you asked about data types I see that startTime is long, so maybe I should do long planksPerHour = ((long)planksCreated * 3600000) / (System.currentTimeMillis() - startTime); Edited June 1, 2014 by blabla123
Realist Posted June 1, 2014 Posted June 1, 2014 You have not supplied enough information. Also, I'm assuming that's not everything you have for determining planksCreated planksCreated += client.getInventory().getAmount(userChoiceP); You haven't even shown us what data types those all are.
Nitrousek Posted June 1, 2014 Posted June 1, 2014 Declared at the beginning, outside any methods. int userChoiceP; long startTime; int planksCreated; And actually that line is all I use for counting planks created and it works fine everytime. As I said before, all other counters work fine. Now that you asked about data types I see that startTime is long, so maybe I should do long planksPerHour = ((long)planksCreated * 3600000) / (System.currentTimeMillis() - startTime); public int getPerHour(int value) { if (System.currentTimeMillis() - startTime > 0) { return (int) (value * 3600000d / System.currentTimeMillis() - startTime); } else { return 0; } } and use in onPaint() like this: g.drawString("" + getPerHour(planksCreated), x,y);
Nitrousek Posted June 1, 2014 Posted June 1, 2014 Try declaring planksCreated as long Don't listen to this guy ^, just use my method, should work. If it doesn't something is wrong with something else you have not shown.
thepecher Posted June 1, 2014 Posted June 1, 2014 Don't listen to this guy ^, just use my method, should work. If it doesn't something is wrong with something else you have not shown. Well fuck you too
TheScrub Posted June 1, 2014 Posted June 1, 2014 (edited) this error occurs when your using an integer and trying to do a large multiplication ie 3600* 600k goes over 2.1b proof: Edited June 1, 2014 by TheScrub
thepecher Posted June 1, 2014 Posted June 1, 2014 this error occurs when your using an integer and trying to do a large multiplication ie 3600* 600k goes over 2.1b proof: Boom you see @Nitrousek told ya!
Nitrousek Posted June 1, 2014 Posted June 1, 2014 Boom you see @Nitrousek told ya! Yes, and my method would work just fine, if you care to test.
Eliot Posted June 1, 2014 Posted June 1, 2014 (edited) As @TheScrub stated, you're going over Integer.MAX_VALUE and thus overflowing. Just change the order of operations. You might have some errors if you store some values as integers, but you should be able to work those out. long planksPerHour = (long)(planksCreated * (3600000.0 / (System.currentTimeMillis() - startTime)); Edited June 1, 2014 by Eliot
Nitrousek Posted June 1, 2014 Posted June 1, 2014 As @TheScrub stated, you're going over Integer.MAX_VALUE and thus overflowing. Just change the order of operations. You might have some errors if you store some values as integers, but you should be able to work those out. long planksPerHour = (long)(planksCreated * (3600000.0 / (System.currentTimeMillis() - startTime)); He can hold planksPerHour as integer, there's no need for long, as it won't even exceed 2k. Also, he doesn't need to reverse order, if he changed the number to double. (3600000.0) or (3600000d). The resulting double will be converted back to integer without any problems. int planksPerHour = (int)(3600000d * planksCreated / System.currentTimeMillis() - startTime);