<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Iroundf on Arcanelab Blog</title><link>https://blog.arcanelab.com/tags/iroundf/</link><description>Recent content in Iroundf on Arcanelab Blog</description><generator>Hugo</generator><language>en-US</language><copyright>Zoltán Majoros</copyright><lastBuildDate>Wed, 29 Sep 2010 04:00:00 +0200</lastBuildDate><atom:link href="https://blog.arcanelab.com/tags/iroundf/index.xml" rel="self" type="application/rss+xml"/><item><title>iroundf A few minutes ago I wrote a rounding function for myself in C.</title><link>https://blog.arcanelab.com/2010/09/iroundf-a-few-minutes-ago-i-wrote-a-rounding-function-for/</link><pubDate>Wed, 29 Sep 2010 04:00:00 +0200</pubDate><guid>https://blog.arcanelab.com/2010/09/iroundf-a-few-minutes-ago-i-wrote-a-rounding-function-for/</guid><description>&lt;h1&gt;iroundf&lt;/h1&gt;
&lt;p&gt;A few minutes ago I wrote a rounding function for myself in C. Probably not the best implementation, but it works all right. If you&amp;rsquo;re interested, check it out (I&amp;rsquo;ve put some sample values in the comments):&lt;/p&gt;
&lt;pre&gt;int iroundf(float value)&lt;br/&gt;{&lt;br/&gt;    if(value &amp;gt; 0.0)&lt;br/&gt;    {        &lt;br/&gt;        int unsigned cutvalue = (unsigned int)value; // 3.8234234 =&amp;gt; 3&lt;br/&gt;        float remain = value - cutvalue; // 3.8234234-3 = 0.8234234&lt;br/&gt;        &lt;br/&gt;        if(remain &amp;gt;= 0.5) // 0.8234234 &amp;gt;= 0.5 =&amp;gt; TRUE &lt;br/&gt;            return cutvalue + 1; // iroundf(3.8234234) =&amp;gt; 4&lt;br/&gt;        else&lt;br/&gt;            return cutvalue;&lt;br/&gt;    }&lt;br/&gt;    &lt;br/&gt;    if(value &amp;lt; 0.0)&lt;br/&gt;    {&lt;br/&gt;        int cutvalue = (int)value; // -1.34 =&amp;gt; -1 // -4.67 =&amp;gt; -4&lt;br/&gt;        float remain = value - cutvalue; // -1.34-(-1) = -0.34 // -4.64-(-4) = -0.64&lt;br/&gt;        &lt;br/&gt;        if(remain &amp;gt;= -0.5) // -0.31 &amp;gt;= -0.5 =&amp;gt; TRUE // -0.61 &amp;gt;= -0.5 =&amp;gt; FALSE&lt;br/&gt;            return cutvalue; // iroundf(-1.34) =&amp;gt; -1&lt;br/&gt;        else&lt;br/&gt;            return cutvalue - 1; // iroundf(-4.67) =&amp;gt; -5&lt;br/&gt;    }&lt;br/&gt;    &lt;br/&gt;    return 0;&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;&lt;/pre&gt;
&lt;p&gt;update: I just came across the following very short and elegant solution (talk about forests and trees&amp;hellip;)&lt;/p&gt;</description></item></channel></rss>