user20207: I made simple sketch that generates three random numbers and then calculates the average of these three random numbers. 
It divides the total by 3 and then displays that average number with serial.println.
Sometimes the generated average number can be decimal (for example 261,333333 or 124,33333).
Interestingly, the serial print doesn't print in decimal format 124,33333
it prints just integer 124.
How do I print the value in decimal?

user7508: In the C family of languages, if you divide an integer by another
integer, the compiler interprets the division operator (/) as an
integer division, i.e. you get the integer part of the quotient. Thus,
(first_int + second_int + third_int) / 3

is an integer.
If either operand is a floating point number, then the other one is
implicitly converted to floating point, and you get a floating point
division. Thus
(first_int + second_int + third_int) / 3.0

is a floating point number, and the result you expect.

user20450: In languages such as C or C++ there is a distinction between integer types and floating-point types. You see, computers work using bits. A bit is either 0 or 1. A bit obviously lack in both range and precision, so we combine multiple bits in order to improve.
Integral Types
A byte is a group of 8 bits. In C, the type used is either
uint8_t x; // Values from 0 to 255.
int8_t  x; // Values from -128 to 127
char    x; // Values from the ASCII character set (still -128 to 127)       

On Ardiuno Uno, an int is a group of 16 bits:
uint16_t c; // Values from 0 to 2^16-1 = 65535, synonym: unsigned int (Arduino)
int16_t  c; // Values from -32768 to 32767,     synonym: int (Arduino)

A long is a group of 32 bits:
uint32_t v; // Values from 0 to 2^32-1 = 4,294,967,295 = 4 GiB (unsigned long)
int32_t  v; // Values from -2,147,483,648 to 2,147,483,647 (long)

There is also usually support for 64-bit numbers, but we try not to use them in Arduino code. The reason is that most Arduino MCUs use 8-bit registers, so 64-bit operations such as "add", "sub", etc. would be very slow.
Note: Different architectures use different numbers of bits for these types. In order to be compatible with other architectures, use int32_t etc. to be sure.
Literals and Casts
Oftentimes, the compiler can not see what type a literal value is supposed to be. In C, the compiler picks a default which is usually larger than you would like. For this reason, it is useful to know how to specify a literal exactly with the type:
 ((uint8_t)200)   // Casting 200 to an unsigned 8-bit integer
 ((char)65)       // Making the ASCII letter 'A'
 ((int32_t)42)    // Force 32 as a signed 32-bit integer (long)
 42L              // The 'L' suffix does the same as above

Division and Remainder
A common cause of problems when programming in C is the division operator /. If the operands are both integer types, the result will also be an integer. So 1/3 is in fact 0. You can get the remainder of the division using the modulus operation %. For instance:
void show_division(int n, int d)
{
    Serial.print("The division: ");
    Serial.print(n);
    Serial.print(" / ");
    Serial.print(d);
    Serial.print(" equals ");
    Serial.print(n / d);
    Serial.print(" with a remainder ");
    Serial.println(n % d);
}

Floating-point Types
All of the above types are integrals, so they do not support any fractional value. Therefore C defines two different floating-point types: float and double. The former is a 32-bit value and the latter is a 64-bit value. The way it works is that the bits are split in 3:

A single bit is used for the sign.
A certain number of bits represent the fractional part, the mantissa.
The rest of the bits are used for the exponent.

As an example, the 32-bit float value representation for 1.25 is
seeeeeee_emmmmmmm_mmmmmmmm_mmmmmmmm = s * 2^(e-127) * (1.m...)_2
00111111_10100000_00000000_00000000 = (+1) * 2^(127-127) * (1.0100)_2
// Note: Binary 1.01 equals 1.25 in decimal

For performance, use the f suffix
Although 64-bit doubles are very accurate, the resulting code will run much slower. I, therefore, recommend using 32-bit floats instead. It is useful to know that you can tack on an f after the floating-point literal:
(a + b + c) / 3.0f; // Make sure to divide using 32-bit operations.

If a, b and c above are integer types, the additions will also be integer additions. When the compiler sees an int divided by a float, it will convert the int to a float, and then perform a float division. This is slow, but certainly faster than a double division.
The compiler is smart, but it doesn't hurt to help it. There is only a very small precision loss in converting this to a float multiplication:
(a + b + c) * (1.0f / 3.0f);

The compiler will pre-calculate (1.0f / 3.0f), so you get a much faster floating-point multiplication (in addition to the int to float cast). 
Note: Arduino Uno defaults all floating point literals to 32-bit, and double is the same as float. Therefore, the f suffix is strictly not needed. It is still good practice to add these for compatibility with other architectures.

20207: <p>I made simple sketch that generates three random numbers and then calculates the average of these three random numbers. </p>

<p>It divides the total by 3 and then displays that average number with <code>serial.println</code>.</p>

<p>Sometimes the generated average number can be decimal (for example 261,333333 or 124,33333).</p>

<p>Interestingly, the serial print doesn't print in decimal format <strong>124,33333</strong>
it prints just integer <strong>124</strong>.</p>

<p>How do I print the value in decimal?</p>
 7508: <p>In the C family of languages, if you divide an integer by another
integer, the compiler interprets the division operator (<code>/</code>) as an
<em>integer division</em>, i.e. you get the integer part of the quotient. Thus,</p>

<pre class="lang-c prettyprint-override"><code>(first_int + second_int + third_int) / 3
</code></pre>

<p>is an integer.</p>

<p>If either operand is a floating point number, then the other one is
implicitly converted to floating point, and you get a floating point
division. Thus</p>

<pre><code>(first_int + second_int + third_int) / 3.0
</code></pre>

<p>is a floating point number, and the result you expect.</p>
 20450: <p>In languages such as C or C++ there is a distinction between integer types and floating-point types. You see, computers work using <strong>bits</strong>. A bit is either <strong>0</strong> or <strong>1</strong>. A bit obviously lack in both range and precision, so we combine multiple bits in order to improve.</p>

<h1>Integral Types</h1>

<p>A <strong>byte</strong> is a group of 8 bits. In C, the type used is either</p>

<pre><code>uint8_t x; // Values from 0 to 255.
int8_t  x; // Values from -128 to 127
char    x; // Values from the ASCII character set (still -128 to 127)       
</code></pre>

<p>On Ardiuno Uno, an <strong>int</strong> is a group of 16 bits:</p>

<pre><code>uint16_t c; // Values from 0 to 2^16-1 = 65535, synonym: unsigned int (Arduino)
int16_t  c; // Values from -32768 to 32767,     synonym: int (Arduino)
</code></pre>

<p>A <strong>long</strong> is a group of 32 bits:</p>

<pre><code>uint32_t v; // Values from 0 to 2^32-1 = 4,294,967,295 = 4 GiB (unsigned long)
int32_t  v; // Values from -2,147,483,648 to 2,147,483,647 (long)
</code></pre>

<p>There is also usually support for 64-bit numbers, but we try not to use them in Arduino code. The reason is that most Arduino MCUs use <strong>8-bit</strong> registers, so 64-bit operations such as "add", "sub", etc. would be very slow.</p>

<p><strong>Note</strong>: Different architectures use different numbers of bits for these types. In order to be compatible with other architectures, use <code>int32_t</code> etc. to be sure.</p>

<h2>Literals and Casts</h2>

<p>Oftentimes, the compiler can not see what type a literal value is supposed to be. In C, the compiler picks a default which is usually larger than you would like. For this reason, it is useful to know how to specify a literal exactly with the type:</p>

<pre><code> ((uint8_t)200)   // Casting 200 to an unsigned 8-bit integer
 ((char)65)       // Making the ASCII letter 'A'
 ((int32_t)42)    // Force 32 as a signed 32-bit integer (long)
 42L              // The 'L' suffix does the same as above
</code></pre>

<h2>Division and Remainder</h2>

<p>A common cause of problems when programming in C is the division operator <code>/</code>. If the operands are both integer types, the result will also be an integer. So <code>1/3</code> is in fact <code>0</code>. You can get the remainder of the division using the modulus operation <code>%</code>. For instance:</p>

<pre><code>void show_division(int n, int d)
{
    Serial.print("The division: ");
    Serial.print(n);
    Serial.print(" / ");
    Serial.print(d);
    Serial.print(" equals ");
    Serial.print(n / d);
    Serial.print(" with a remainder ");
    Serial.println(n % d);
}
</code></pre>

<h1>Floating-point Types</h1>

<p>All of the above types are integrals, so they do not support any fractional value. Therefore C defines <strong>two</strong> different floating-point types: <strong>float</strong> and <strong>double</strong>. The former is a 32-bit value and the latter is a 64-bit value. The way it works is that the bits are split in 3:</p>

<ul>
<li>A single bit is used for the <strong>sign</strong>.</li>
<li>A certain number of bits represent the fractional part, the <strong>mantissa</strong>.</li>
<li>The rest of the bits are used for the <strong>exponent</strong>.</li>
</ul>

<p>As an example, the 32-bit float value representation for 1.25 is</p>

<pre><code>seeeeeee_emmmmmmm_mmmmmmmm_mmmmmmmm = s * 2^(e-127) * (1.m...)_2
00111111_10100000_00000000_00000000 = (+1) * 2^(127-127) * (1.0100)_2
// Note: Binary 1.01 equals 1.25 in decimal
</code></pre>

<h2>For performance, use the <code>f</code> suffix</h2>

<p>Although 64-bit doubles are very accurate, the resulting code will run much slower. I, therefore, recommend using 32-bit floats instead. It is useful to know that you can tack on an <code>f</code> after the floating-point literal:</p>

<pre><code>(a + b + c) / 3.0f; // Make sure to divide using 32-bit operations.
</code></pre>

<p>If <code>a</code>, <code>b</code> and <code>c</code> above are integer types, the additions will also be integer additions. When the compiler sees an <code>int</code> divided by a <code>float</code>, it will convert the <code>int</code> to a <code>float</code>, and then perform a float division. This is slow, but certainly faster than a double division.</p>

<p>The compiler is smart, but it doesn't hurt to help it. There is only a very small precision loss in converting this to a float multiplication:</p>

<pre><code>(a + b + c) * (1.0f / 3.0f);
</code></pre>

<p>The compiler will pre-calculate <code>(1.0f / 3.0f)</code>, so you get a much faster floating-point multiplication (in addition to the <code>int</code> to <code>float</code> cast). </p>

<p><strong>Note</strong>: Arduino Uno defaults all floating point literals to 32-bit, and <code>double</code> is the same as <code>float</code>. Therefore, the <code>f</code> suffix is strictly not needed. It is still good practice to add these for compatibility with other architectures.</p>
