For pitching statistics in baseball we have the following:
1/3 equals 1 out, 2/3 equals 2 outs, and 3/3 equals 3 outs.
When you have 3 outs it is considered 1 complete inning.
If you have 5 outs, it is considered 1 inning and 2/3 thirds of an inning.
In excel in a column I put the innings that a pitcher throws per game
As you can see in the table, pitcher Juanito Perez pitched 4 times, the first time he pitched 2/3, the second 1/3, the third 5 2/3 and the last 3 1/3, all this means that He has a total of 10 innings pitched.
What I need is the following, now instead of handling fractions I have to use decimals, that is, now 1/3 is equal to .1, 2/3 is equal to .2 and 3/3 is equal to 1.
In other words, taking the example of the table, now I would have to put it like this:
How can I make that sum?
I made this code in C#
double[] entradas = { 0.2, 0.1, 5.2, 3.1 };
int enteros = 0;
double tercios = 0.0;
for(int i = 0; i < 4; i++)
{
enteros += (int)Math.Truncate(entradas[j]);
double num1 = tercios - Math.Truncate(tercios);
double num2 = entradas[j] - Math.Truncate(entradas[j]);
double tercio = num1 + num2;
if (tercio == 0.3)
{
if(Math.Truncate(tercios) > 0)
tercios += (0.3 * 10) / 3;
else
tercios = (0.3 * 10) / 3;
}
else
tercios += (entradas[j] - Math.Truncate(entradas[j]));
}
double total = enteros + tercios;
Console.WriteLine(total);
Console.ReadKey();
The problem here is that when you add .2 + .1 it gives me 0.30000000000000004 and logically the condition is not met, why is it giving me that result when it should give me 0.3?
You can't do it with a single formula, you can do something like this:
Column B uses the INT function to output the integer part of the number. Column C uses this formula:
=(A1-TRUNC(A1))*3.33
. Then in the totals you just round the result of column C and add it to column B.The first thing you need to do is get the integer part of the episode and the decimal part of the episode and add them each in separate accumulators. Then the sum of the decimal part or thirds must verify if it is divisible by 3, but all this can be done with a simple formula.
The example that I leave you is extremely easy, inside the loop the only thing that is done is to obtain the integer values of the episode and add them. The same is done with the thirds with the difference multiplied by ten (10) to obtain the whole value of the decimal point of the third. At the end, the sum of both the episodes and the thirds is obtained.
Now the important part is the total calculation of the episodes:
episodiosCompletos
+ Math.Truncate(terciosCompletos / 3D) + ((terciosCompletos % 3) / 10D)
Previously calculated episodes within the loop.episodiosCompletos +
Math.Truncate(terciosCompletos / 3D)
+ ((terciosCompletos % 3) / 10D)
Now the number of thirds is divided by 3 and the whole value of it is extracted, with this we obtain the number of episodes that are within the thirds.episodiosCompletos + Math.Truncate(terciosCompletos / 3D) +
((terciosCompletos % 3) / 10D)
This part is important, since, if the sum of the thirds is not a value divisible by 3, then the rest of the division by 3 is obtained to obtain the third of the remaining episode, but since an integer is returned, it ends up dividing by 10.The same applies if you pass it to an excel.
Result:
Formulas: The same calculations that are done in C #. Note that in excel the function is used
RESIDUO
(Its counterpart in English is MOD) and in C# the percentage symbol (%
) to obtain the remainder of the division.Updating: As I say in the comments (I don't program in php), but, the doubt arose as to why it gives you other decimals, so I searched the internet for how to create a
array
, how to use afor
and what is the counterpart ofMath.Truncate
in php, so I turned to aphp fiddle online
and managed to do this. So far it has been working fine for me.