Note that there are some explanatory texts on larger screens.

plurals
  1. POHelp optimizing algorithm
    text
    copied!<p>I have a particular application that needs to calculate something very specific, and while I excel at logic, math has always been my weak spot.</p> <p>Given a number, say -20, it needs to apply a calculation of a 100 base (that is, the base is 100, not 0. not to be confused with base 100 which would be something else totally).</p> <p>In other words, the math works like this ..., 105, 104, 103, 102, 101, 100, -101, -102, -103, -104, -105, ...</p> <p>Then I need to do the math based on this 100 base figure.</p> <p>So, for example:</p> <pre><code>-140 - 20 = 120 -120 - 20 = 100 -115 - 20 = -105 -110 - 20 = -110 -105 - 20 = -115 100 - 20 = -120 120 - 20 = -140 </code></pre> <p>If it helps, the problem is related to sports odds. In theory, this refers to money (risk $1.40 to win $1.00 on one side, risk $1.00 to win $1.20 on the other, the .20 difference is what casinos call "the juice" or their cut of moving money back and forth.) However, the program is not dealing with real money, it's more of a simulator.</p> <p>My current formula works like this:</p> <pre><code>decimal CalculateSides(decimal side, decimal vig) { decimal newSide = side - vig; newSide = -(newSide &lt; 0) ? newSide + 100 : newSide - 100; return (newSide &lt; 0) ? newSide + 100 : newSide - 100; } </code></pre> <p>While this formula works, I'm not very happy with the conditional +/-, but this is basically the only formula that works for me. </p> <p>Can anyone suggest a way to improve this? Preferably without conditions of +/-?</p> <p>EDIT: </p> <p>When I asked the question, I knew one possible answer was "It's probably best the way you're doing it", and that seems to be the consensus.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload