title: Get decimal part of a number
body: <p>Trying to get whole and decimal part of a number into two variables. What I tried:</p>
  
  <pre><code>#include &lt;iostream&gt;
  int main(){
      float n,m; int k; std::cin &gt;&gt; n;
      k = n;
      m = n - k;
  </code></pre>
  
  <p>Tried to convert the float number to int and received warning by the compiler that the number could be incorrect, tested, was incorrect indeed and couldn't get the expected results. Searched on this, couldn't find any other workaround than using <code>floor()</code>.</p>
  
  <p>My actual code:</p>
  
  <pre><code>int main() {
      float n; cin &gt;&gt; n;
      int nfloor = n;
      cout &lt;&lt; nfloor &lt;&lt; "\n";
      float nfloat = n - nfloor; int nfloatfloor;
      cout &lt;&lt; nfloat &lt;&lt; "\n";
      do {
          nfloat *= 10;
          nfloatfloor = nfloat;
          cout &lt;&lt; nfloat &lt;&lt; "\n" &lt;&lt; nfloatfloor &lt;&lt; "\n";
      } while (nfloat &gt; nfloatfloor);
  }
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Input: 12.34
  Output :
  12
  0.34
  3.4
  3
  34
  34
  340
  340
  3400
  3400
  34000
  34000
  340000
  340000
  3.4e+06
  3400001
  3.4e+07
  34000016
  </code></pre>
  
  <p>Subtracting two float numbers returns an incorrect value, Searched on this but the answers were on a high level that I couldn't understand.</p>
  
  <p>My actual code:</p>
  
  <pre><code>int main() {
      float n; cin &gt;&gt; n;
      float nfloor = floor(n);
      cout &lt;&lt; nfloor &lt;&lt; "\n";
      float nfloat = n - nfloor; float nfloatfloor;
      cout &lt;&lt; nfloat &lt;&lt; "\n";
      do {
          nfloat *= 10;
          nfloatfloor = floor(nfloat);
          cout &lt;&lt; nfloat &lt;&lt; "\n" &lt;&lt; nfloatfloor &lt;&lt; "\n";
      } while (nfloat &gt; nfloatfloor);
  }
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>Input: 12.34
  Output:
  12
  0.34
  3.4
  3
  34
  34 //Run should stop here because of the while loop bit it doesn't, funny thing is it gives me different results sometimes, last time it gave me 35 and 34
  340
  340
  3400
  3400
  34000
  34000
  340000
  340000
  3.4e+06
  3.4e+06
  3.4e+07
  3.4e+07
  </code></pre>
  
  <p>@Slava Take a look at the output right above this sentence, compiler printed 34 and 34, the duplicate answer shows that the couts are  34.0000000000000004 or something like that, and as I commented above, the code should have stopped, what I'm really trying to do is compare a float number and int number, if (float >int) the code should continue and if not it should stop so is there any solution?
  @hnefatl I tried your answer and the compiler just hangs:</p>
  
  <pre><code>int main() {
      float n2, whole, fractional, fractional2, whole2; cin &gt;&gt; n2;
      int denominator = 1;
      fractional = modf(n2, &amp;whole);
      do {
          fractional *= 10;
          fractional2 = modf(fractional, &amp;whole2);
          denominator *= 10;
      } while (fractional &gt; fractional2);
      if (denominator &gt; 1)
          denominator /= 10;
      cout &lt;&lt; denominator;
  }
  </code></pre>