SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with a little math please

    once again, more new ground for me. I probably need some obscure code to get this right. any thoughts please?

    have a difference of two timestamps... diff in seconds, ex. 1209600.
    I divide by whatever it is to convert to full days, say 14 days.
    I also have a few "HIT" frequencies, say one's every 7 days.

    NOW what i'm trying to do is this:

    look at a particular diff/frequency combo, and figure if it divides evenly into a whole number without any remainder. 14/7 = 2 (a HIT), 1358/14 = 97 (a HIT), etc...

    code would look like this:
    if (diff = 0) or (diff/frequency = a whole number){a HIT}

    that's the ideal, but... now for extra credit...
    for some reason my diffs are not exact, but real close - ALWAYS within 1%.

    so what will do is this:
    if (diff = 0) or (diff/frequency = a whole number +\- 1%){a HIT}

    figure solution will run negative values too.

    so there you have it. any and all comments are greatly appreciated! thank you!!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Part 1

    Does y divide x evenly?

    Two solutions:

    1) (x % y == 0) will be true. % is the modulus operator and gives the remainder of integer division (like how you did long division on paper in elementary school). The remainder is 0 if two numbers divide evenly.

    2) ((x / y) == round(x / y)) will be true. If the result of division had a fractional part, then it would not be equal to the rounded version of that result, which by definition never has a fractional part.

    Part 2

    This depends on what you want to be within +/- 1% of. The divisor or the quotient?

    One possibility:

    ((x % y) / y) < 0.01

    Or:

    ((x / y) - floor(x / y)) < 0.01

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    Part 1

    Does y divide x evenly?

    Two solutions:

    1) (x % y == 0) will be true. % is the modulus operator and gives the remainder of integer division (like how you did long division on paper in elementary school). The remainder is 0 if two numbers divide evenly.

    2) ((x / y) == round(x / y)) will be true. If the result of division had a fractional part, then it would not be equal to the rounded version of that result, which by definition never has a fractional part.

    Part 2

    This depends on what you want to be within +/- 1% of. The divisor or the quotient?

    One possibility:

    ((x % y) / y) < 0.01

    Or:

    ((x / y) - floor(x / y)) < 0.01
    % SWEET!
    thanks a lot Dan, very helpful, all of it... you rock !

  4. #4
    SitePoint Zealot
    Join Date
    Oct 2009
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($difference % $every == 0){
    echo "<br>" . "A HIT";}

    works fine.

    if ((($difference / $every) - floor($difference / $every)) < 0.01){
    echo "<br>" . "A HIT";}

    works fine when $difference is < zero ONLY.

    so I added this: seems to be working fine.

    if ((ceil($difference / $every) - ($difference / $every)) < 0.01){
    echo "<br>" . "A HIT";}

    thanks again for the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •