Jaked222 Jaked222 - 1 year ago 75
Android Question

Logical brain teaser involving mod operator, help me please?

I think this should be pretty easy but I'm being stumped. I have a float being stored in a database (lets call it N) that is being updated every 5-10 seconds, always increasing by some unknown amount (usually between 0.0 and 200.0). I need to display a "milestone" that occurs every time the number goes past an interval of 304.8.

Trying to use mod (304.8 % N) to check when it's 0, that doesnt work, because N could go from 302.2 to 308.6 for example, never having a 0 remainder.

I want to compute the number of milestones reached (every time 304.8 is passed) and also give a notification to the user each time. For some reason I can't think of a reasonable way to do this.

The problem is occurring in my android (java) program.

Answer Source

You could to divide n by the milestone and floor the result. Then compare it to the previous result. If the new one is larger than the previous one, you've passed a milestone:

double interval = 304.8;
double prev = // saved from the previous round
double curr = prev + someIncrement();
if (Math.floor(curr/interval) > Math.floor(prev/interval)) {
prev = curr; // For next round
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download