user37735: Suppose I want to divide two integer variables and put the result into a float.
In order to prevent integer division do I have to program like this:
    int i = 6312;
    int j = 258;
    float f;
    f = i;
    f = i/j;

Or can I straightforward program like below and the compiler makes sure it is handled the 'proper' way:
    f = i/j;


user6936: As Arduino is C++, you should really use a static cast, to be technically accurate. However, for Arduino code it probably doesn't really matter.
So either
float f = static_cast< float >(i) / static_cast< float >(j);

or
float f = static_cast< float >(i) / j;

Note: Using a static cast on each operand, rather than just one, is  useful to quiet any warnings about the loss of precision with int to float, which typically needs to form a rounded float for large int values.
See C++ int float casting for more info.

Just to add to Jot's answer: Strictly speaking, it should be pointed out that (float) is C, whereas float() is C++.

Also, as you had meant to write in your question:

int i = 6312;
int j = 258;
float f;
f = i;
f = f/j;
Yes, that would work. However, even though it seems quite elegant, it could cause confusion later in time, when someone else examines the code - unless you added a comment explaining why.
it is probably best to keep the code simple and obvious (for debugging purposes) unless you are deliberately intending to obfuscate the code.

user30198: Cast the integers to float before doing the calculation.
float f = float(i) / float(j);

or
float f = (float) i / (float) j;

Always be clear how the calculation should be done, so the compiler will do what you want it to do.

37735: <p>Suppose I want to divide two integer variables and put the result into a float.
In order to prevent integer division do I have to program like this:</p>

<pre><code>    int i = 6312;
    int j = 258;
    float f;
    f = i;
    f = i/j;
</code></pre>

<p>Or can I straightforward program like below and the compiler makes sure it is handled the 'proper' way:</p>

<pre><code>    f = i/j;
</code></pre>
 6936: <p>As Arduino is C++, you should <em>really</em> use a <a href="https://stackoverflow.com/a/1255015/2628257">static cast</a>, to be technically accurate. However, for Arduino code it probably doesn't really matter.</p>
<p>So either</p>
<pre><code>float f = static_cast&lt; float &gt;(i) / static_cast&lt; float &gt;(j);
</code></pre>
<p>or</p>
<pre><code>float f = static_cast&lt; float &gt;(i) / j;
</code></pre>
<p><strong>Note</strong>: Using a static cast on each operand, rather than just one, is  useful to quiet any warnings about the loss of precision with <code>int</code> to <code>float</code>, which typically needs to form a rounded <code>float</code> for large <code>int</code> values.</p>
<p>See <a href="https://stackoverflow.com/q/5456801/4424636">C++ int float casting</a> for more info.</p>
<hr />
<p>Just to add to Jot's answer: Strictly speaking, it should be pointed out that <code>(float)</code> is C, whereas <code>float()</code> is C++.</p>
<hr />
<p>Also, as you had meant to write in your question:</p>
<pre>
int i = 6312;
int j = 258;
float f;
f = i;
<strong>f = f/j;</strong></pre>
<p>Yes, that would work. However, even though it seems quite elegant, it could cause confusion later in time, when someone else examines the code - unless you added a comment explaining why.</p>
<p>it is probably best to keep the code simple and obvious (for debugging purposes) unless you are deliberately intending to obfuscate the code.</p>
 30198: <p>Cast the integers to float before doing the calculation.</p>

<pre><code>float f = float(i) / float(j);
</code></pre>

<p>or</p>

<pre><code>float f = (float) i / (float) j;
</code></pre>

<p>Always be clear how the calculation should be done, so the compiler will do what you want it to do.</p>
