-11

I'm in need of a formula in any programming language that would give me a value that I could ultimately sort by. I have 2 variables:

  1. variable 1 = just some arbitrary count (could be something like 0, 1, 5, 20, 50, 100)
  2. variable 2 = number of days that passed since current date (could be 0, 1, 2, 50, 100)

I'm in need of a weighted custom formula where:

  1. between 0-30 days from current date, variable 1 (count) starts at 100% weight and variable 2 at 0% weight. As each day passes up to 30 days, variable 1 weight goes down to 50% on day 30 and variable 2 (number of days passed) increases from 0% to 50% on day 30
  2. between 31-60 days from current date, variable 1 starts increasing from 50% to 100% peaking at day 60 and variable 2 starts decreasing to 0 and goes to 0 on day 60
  3. beyond day 60, variable 1 is always at 100% and variable 2 obviously at 0%
7
  • 3
    In what programming language? If you just need a mathematical formula you're on the wrong site.
    – SeinopSys
    Aug 3, 2015 at 14:42
  • 6
    It's a general tag related to mathematics if you hover over it and read the tag wiki summary. It must be accompanied by a language tag. Just because a tag exists, it doesn't mean it can be used by itself.
    – SeinopSys
    Aug 3, 2015 at 15:04
  • I'm not asking a question about the meaning of life. the question accurately represents a need for a mathematical formula in any programming language
    – TimJohnson
    Aug 3, 2015 at 15:05
  • 3
    This question is currently being discussed on meta
    – user4639281
    Aug 3, 2015 at 15:18
  • 1
    @TimJohnson if you would like to discuss what you can do to improve this post, a good place to do it would be in that meta discussion.
    – user4639281
    Aug 3, 2015 at 16:10

1 Answer 1

2

Let fabs be a function that returns the absolute value of a float. Then the formula could look like this:

function calcRank(float v1, float v2) {
    float k = fabs(v2-30.0);
    if (k < 30.0) {
        // Rule 1 and 2
        rank = k/60.0 + (60.0-k) * v1 / 60.0;
    } else {
        // Rule 3
        rank = v1;
    }
    return rank;
}
3
  • thank you. one thing I noticed, if v1 is say 10 and v2 changes from 10 days to say 20 days, you get: 1) abs(10 - 30)/60.0 + (60.0-abs(10-30)) * 10 / 60.0 = 7 2) abs(20 - 30)/60.0 + (60.0-abs(20-30)) * 10 / 60.0 = 8.5 I think 1st should be higher, no?
    – TimJohnson
    Aug 3, 2015 at 15:35
  • this is my fault as I did not clarify, in my case, as days go by and v1 stays the same, the ranking should be lower, not higher. Also, if v2 stays constant and v1 increases, the ranking should be higher as well. The above formula does not account for that, obviously because I forgot to include it. If you can update the formula, I'd appreciate it
    – TimJohnson
    Aug 3, 2015 at 15:42
  • Try using rank = (60.0-k) * v1 / 60.0; for rule 1 and 2. Aug 4, 2015 at 6:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.