germanivanov0719 / grades

Website to make you worry less about your grades
https://germanivanov0719.github.io/grades/
MIT License
1 stars 0 forks source link

Optimize calculations without rounding (use a formula instead of a loop) #18

Open germanivanov0719 opened 1 year ago

germanivanov0719 commented 1 year ago

Issue

When creating this app, I used a brute-force approach for calculating required number of new grades to reach a certain goal. While it does allow me to add the option to calculate with rounding, it greatly decreases performance and limits the number of available goals and marks used to reach those goals.

I knew about this design limitation, and it has to be fixed as some point. I will have to come up with a solution on how to integrate the fix to this app.

Status Tracking

germanivanov0719 commented 1 year ago

Math for the calculations

Variables available

Right now, there are a few basic variables. Here is a table with proposed math names (for formulas), descriptions, current (ver. d44e2b0) and future names in code for each:

Math Variable Description Current name Future name
$M_0$ Original mean value M1 M0
$W_0$ Original weight sum s W0
$x$ New grade value m x
$w$ New grade weight w w
$M_1$ New mean value M2 M1
$W_1$ New weight sum Not used W1

Formulas

Let's define basic formulas for variables in the table above.

$$ W_1 = W_0 + w \ \ $$

$$ M_1 = \frac{M_0 \cdot W_0 + x \cdot w}{W_0 + w} $$

From the second formula, we can find $w$:

$$ w = \frac{W_0 (M_0 - M_1)}{M_1 - x} $$

Assuming rounding up to the nearest integer from $M_1$ including (therefore, making it the closest value we have to reach), and only solving for integer values of $w$ (as it is the weight, which is often a whole number):

$$ w = \left\lceil {\frac{W_0 (M_0 - M_1)}{M_1 - x}} \right \rceil $$

What's different

  1. Make sure to use the correct $M_1$ value when going down (e. g. you need at least $2.49$ to round down, not $2.50$, when using the normal math rounding)
  2. If $M_1 < M_0$, the $w$ value is still positive, which is different from the current approach. Maybe it is worth getting rid of the negative sign as it may be not that intuitive for someone, and just put a warning or a text label instead.
  3. If the calculated $w$ is negative, then the input makes no sense. For example, if you are trying to reach $M_1 = 5.0$ with $x = 4$ and $M_0 < M_1$.
  4. Rounding up (ceiling) is important. Do not use normal rounding or rounding down for this approach.

Opportunities

  1. This approach allows the user to specify more conditions for the calculation.

    For example, right now you can only use $x = 3$ for calculations, when looking for $M_1$ rounded to $3$. This approach also allows using $2$'s, $1$'s, zeros, and even negative and/or non-integer numbers to reach the same rounded $M_1$ value.

  2. It allows users to specify the set of grades their school uses for grading.

    It is possible to add a manual entry, or just provide a range selection for users in settings, which would later be displayed on the main page.

  3. Calculating will be much faster even for the huge values.

    This approach takes approximately constant time, and it means users will be able to recalculate as many times as they want without worrying about performance.

Possible implementations

I'm still thinking about which interface should be used to support most of the features of the new approach. However, it is likely I'll save current view at least for calculations with roundings.

germanivanov0719 commented 1 year ago

Scheduled for 2023-07-13 – 2023-07-20