1

For pitching statistics in baseball you 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 a full inning.

If you have 5 outs, it is considered 1 inning and 2/3 thirds of inning.

In excel in a column I put the innings thrown by a pitcher per game

As you can see in the table the pitcher Juanito Perez pitched 4 times, in the first time he threw 2/3, in the second 1/3, in the third 5 2/3 and in the last 3 1/3, all this gives that has as total 10 innings thrown.

What I need is the following, now instead of handling fractions I have to use decimals ie now 1/3 equals .1, 2/3 equals .2 and 3/3 equals 1.

In other words, taking the example of the table, I would now 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++)
{

double num1 = tercios - Math.Truncate(tercios);
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
}

double total = enteros + tercios;

Console.WriteLine(total);
``````

The problem here is that when you add .2 + .1 you give me 0.30000000000000004 and logically the condition is not fulfilled, why are you giving me that result when I should give 0.3?

asked by Jorge Alonso 03.01.2019 в 20:11
source

0

The first thing you should do is get the whole 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 should verify if it is divisible by 3, but all this can be done with a simple formula.

``````double[] entradas = { 0.2, 0.1, 5.2, 3.1 };

int episodiosCompletos = 0;
int terciosCompletos = 0;

int episodio = 0;
int tercio = 0;

for (int i = 0; i < entradas.Length; i++)
{
tercio = (int)(((entradas[i] - episodio) + 0.05) * 10);

episodiosCompletos += episodio;
terciosCompletos += tercio;
}

totalEpisodiosJugados = episodiosCompletos + Math.Truncate(terciosCompletos / 3D) + ((terciosCompletos % 3) / 10D);

Console.WriteLine(string.Format("Episodios: {0}\nTercios: {1}\nTotal: {2}", episodiosCompletos, terciosCompletos, totalEpisodiosJugados));
``````

The example that I leave to 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 being multiplied by ten (10) to obtain the whole value of the decimal point of the third. At the end you get the sum of both the episodes and the thirds.

Now the important part is the total calculation of the episodes:

`episodiosCompletos` `+ Math.Truncate(terciosCompletos / 3D) + ((terciosCompletos % 3) / 10D)` Episodes previously calculated within the loop.

`episodiosCompletos +` `Math.Truncate(terciosCompletos / 3D)` `+ ((terciosCompletos % 3) / 10D)` Now divided by 3 the number of thirds and the whole value is extracted from it, with this we get 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 divisible value between 3, then the rest of the division is obtained by 3 to obtain the third of the remaining episode, but as an integer is returned, it is divided by 10.

The same applies if you pass it to an excel.

Formulas: The same calculations that are made in C #. Note that the function `RESIDUO` is used in Excel (Its counter part in English is MOD) and in C # the percentage symbol ( `%` ) to obtain the rest of the division. ``````Entradas    Episodio        Tercio
0.2         =TRUNCAR(A2)    =(A2-B2)*10
0.1         =TRUNCAR(A3)    =(A3-B3)*10
5.2         =TRUNCAR(A4)    =(A4-B4)*10
3.1         =TRUNCAR(A5)    =(A5-B5)*10

=SUMA(B2:B6)    =SUMA(C2:C6)    =B7 + TRUNCAR(C7/3) + (RESIDUO(C7,3) / 10)
Episodios C.    Tercios C.      Total Episodios Jugados
``````

Updating: As I say in the comments (I do not program in php), but, the question arose because it gives you decimals, so look on the internet how you create a `array` , how to use a `for` and what is the counterpart of `Math.Truncate` in php, so I turned to a `php fiddle online` and managed to do this. So far I am working well.

``````<?php

\$entradas = array(0.2, 0.1, 5.2, 3.1);

\$episodiosCompletos = 0;
\$terciosCompletos = 0;

\$episodio = 0;
\$tercio = 0;

for(\$i = 0; \$i < count(\$entradas); \$i++){
\$tercio = (\$entradas[\$i] - \$episodio) * 10;

\$episodiosCompletos += \$episodio;
\$terciosCompletos += \$tercio;
}

\$totalEpisodiosJugados = \$episodiosCompletos + intval(\$terciosCompletos / 3) + ((\$terciosCompletos % 3) / 10);

echo "Episodios: ", \$episodiosCompletos, ", ";
echo "Tercios: ", \$terciosCompletos, ", ";

?>
``````

Column B uses the INT function to get the whole 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.