-1
$\begingroup$

I am trying to calculate an additive value for required tasks only in my application and am having a hard time with the math.

In my application I have a total of 13 tasks that are optional (represented below by $O$). I will know how many are going to be executed before execution begins. I have a varying number of required tasks that is also known before execution begins represented below by $R$.

When each optional task is executed it increments total progress by $1$. I need to calculate how much to add when required tasks complete.

So a basic coding example:

int Progress = 0;
double optionalTasks = 13;
double requiredTasks = 77;
double additive = ?; // Current math is (requiredTasks / (100 - optionalTasks)) * 100

foreach (Task t in Tasks) {
    if (t.Optional)
        Progress++;
    else
        Progress += (int)additive;
}

The current calculation for the additive value is: $$\mathbf{A = \left(\frac{R}{100 - O}\right) \cdot 100}$$

This is just not correct and I am trying to understand what I am doing wrong here.

EDIT


So after receiving help through comments and looking over my code, I figured out that the issue here was multiplying by 100 for the additive value before adding it to progress, also, since this value can be greater than $0$ but less than $1$ it needed to be stored in a temporary variable before assigning it back to the total progress in the end.

THANK YOU


Thank you all for your help, and also to the administrators for applying the appropriate tags. I wish I could make this post clearer for future readers, and if anyone has any ideas please feel free to let me know. This boiled down to simply overlooking an extra step that wasn't needed.

$\endgroup$
13
  • $\begingroup$ do you know the total number of tasks to be executed before the first task runs? $\endgroup$
    – Vasili
    Apr 11, 2018 at 13:07
  • $\begingroup$ Yes, sorry, I forgot to add that value. Updating question. $\endgroup$ Apr 11, 2018 at 13:09
  • 1
    $\begingroup$ Impossible to understand... $\endgroup$ Apr 11, 2018 at 13:09
  • $\begingroup$ You say that tasks 01,02 and 03 are optional. Thus, how to compute the progress if we do not know what happens ? $\endgroup$ Apr 11, 2018 at 13:10
  • $\begingroup$ I am trying to calculate the value to add for 't'. $\endgroup$ Apr 11, 2018 at 13:10

1 Answer 1

0
$\begingroup$

To solve for the additive value (represented by $A$) simply divide the number of required tasks (represented by $R$) by $100$ minus the number of optional tasks (represented by $O$), and this will give you the percentage each required task is responsible for.

$$\mathbf{A = \frac{R}{100 - O}}$$

Since $A$ is a decimal type with a value that can be greater than $0$ but less than $1$, you'll have to keep track of the total percentage as a decimal type; then convert it to integer before assigning it to the progress control. So in coding terms:

int OptionalTasks = 13;
int RequiredTasks = 77;
int Progress = 0;
double tempProgress = 0;
double requiredPercentage = RequiredTasks / (100 - OptionalTasks);

foreach (Task t in Tasks) {
    if (t.Required)
        tempProgress += requiredPercentage;
    else
        tempProgress++;

    Progress = (int)tempProgress;
}

I ended up using @Vasya's answer in the comments above and just adding it to the existing percentage (represented by $P$) after summing all tasks (represented by $T$). $$\mathbf{A = \frac{100}{T}}$$ $$\mathbf{P = P + A}$$

In the end this ended up being the more ideal approach as less math was involved. The number of optional tasks still has to be calculated before the math can be performed, but it allows the additive value to be constant across all tasks, thus reducing confusion for future developers.

int OptionalTasks = 13;
int RequiredTasks = 77;
int Progress = 0;
double tempProgress = 0;
double individualProgress = 100 / (OptionalTasks + RequiredTasks);

foreach (Task t in Tasks) {
    tempProgress += individualProgress;
    Progress = (int)tempProgress;
}

Also, to put into perspective why this calculation is necessary prior to executing the tasks, addition is faster than all other mathematical operations. When I executed 1 million tasks (just calculating the percentage and writing it to the screen) with division calculations after every task, it took a little over 1 minute. When done with pure addition during task execution it took 23 seconds. Less than half the time.

Thank you all for your help, and hopefully this answer will help others in the future.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .