<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: maths code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 12:12:14 GMT</pubDate>
    <description>DZone Snippets: maths code</description>
    <item>
      <title>Gaussian/Banker's Rounding</title>
      <link>http://snippets.dzone.com/posts/show/1305</link>
      <description>Gaussian rounding (aka Banker's rounding) rounds numbers in such a way as there is no upwards bias when rounding x.5. This minimises cumulative error in summing, averaging, &amp;c. rounded numbers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/**&lt;br /&gt; * Gaussian rounding (aka Banker's rounding) is a method of statistically&lt;br /&gt; * unbiased rounding. It ensures against bias when rounding at x.5 by&lt;br /&gt; * rounding x.5 towards the nearest even number. Regular rounding has a&lt;br /&gt; * built-in upwards bias.&lt;br /&gt; */&lt;br /&gt;function gaussianRound(x) {&lt;br /&gt;    var absolute = Math.abs(x);&lt;br /&gt;    var sign     = x == 0 ? 0 : (x &lt; 0 ? -1 : 1);&lt;br /&gt;    var floored  = Math.floor(absolute);&lt;br /&gt;    if (absolute - floored != 0.5) {&lt;br /&gt;        return Math.round(absolute) * sign;&lt;br /&gt;    }&lt;br /&gt;    if (floored % 2 == 1) {&lt;br /&gt;        // Closest even is up.&lt;br /&gt;        return Math.ceil(absolute) * sign;&lt;br /&gt;    }&lt;br /&gt;    // Closest even is down.&lt;br /&gt;    return floored * sign;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Thanks to Dennis W Jay for pointing out a bug in the code.</description>
      <pubDate>Fri, 27 Jan 2006 20:47:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1305</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
  </channel>
</rss>
