<!-- 
RSS generated by JIRA (4.4#649-r158309) at Sun May 19 00:50:43 CDT 2013

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary add field=key&field=summary to the URL of your request.
For example:
http://dev.clojure.org/jira/si/jira.issueviews:issue-xml/CLJ-1166/CLJ-1166.xml?field=key&field=summary
-->
<rss version="0.92" >
<channel>
    <title>Clojure JIRA</title>
    <link>http://dev.clojure.org/jira</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>4.4</version>
        <build-number>649</build-number>
        <build-date>25-07-2011</build-date>
    </build-info>

<item>
            <title>[CLJ-1166] Range function accumulates minor errors when called on floating-point numbers</title>
                <link>http://dev.clojure.org/jira/browse/CLJ-1166</link>
                <project id="10010" key="CLJ">Clojure</project>
                        <description>&lt;p&gt;Due to range&apos;s incremental computation minor errors introduced by floating point arithmetic accumulate, becoming more noticeable in long ranges and causing unexpected behaviour.&lt;/p&gt;

&lt;p&gt;Compare the output of the following:&lt;/p&gt;

&lt;p&gt;=&amp;gt; (range 0.0 10.0 0.1)&lt;br/&gt;
(0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 1.6000000000000003 1.7000000000000004 1.8000000000000005 1.9000000000000006 2.0000000000000004 2.1000000000000005 2.2000000000000006 2.3000000000000007 2.400000000000001 2.500000000000001 2.600000000000001 2.700000000000001 2.800000000000001 2.9000000000000012 3.0000000000000013 3.1000000000000014 3.2000000000000015 3.3000000000000016 3.4000000000000017 3.5000000000000018 3.600000000000002 3.700000000000002 3.800000000000002 3.900000000000002 4.000000000000002 4.100000000000001 4.200000000000001 4.300000000000001 4.4 4.5 4.6 4.699999999999999 4.799999999999999 4.899999999999999 4.999999999999998 5.099999999999998 5.1999999999999975 5.299999999999997 5.399999999999997 5.4999999999999964 5.599999999999996 5.699999999999996 5.799999999999995 5.899999999999995 5.999999999999995 6.099999999999994 6.199999999999994 6.299999999999994 6.399999999999993 6.499999999999993 6.5999999999999925 6.699999999999992 6.799999999999992 6.8999999999999915 6.999999999999991 7.099999999999991 7.19999999999999 7.29999999999999 7.39999999999999 7.499999999999989 7.599999999999989 7.699999999999989 7.799999999999988 7.899999999999988 7.999999999999988 8.099999999999987 8.199999999999987 8.299999999999986 8.399999999999986 8.499999999999986 8.599999999999985 8.699999999999985 8.799999999999985 8.899999999999984 8.999999999999984 9.099999999999984 9.199999999999983 9.299999999999983 9.399999999999983 9.499999999999982 9.599999999999982 9.699999999999982 9.799999999999981 9.89999999999998 9.99999999999998)&lt;/p&gt;

&lt;p&gt;=&amp;gt; (defn range&apos; &lt;span class=&quot;error&quot;&gt;&amp;#91;start end step&amp;#93;&lt;/span&gt; (map #(+ start (* % step)) (range 0 (/ (- end start) step) 1)))&lt;br/&gt;
=&amp;gt; (range&apos; 0.0 10.0 0.1)&lt;br/&gt;
(0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1.0 1.1 1.2000000000000002 1.3 1.4000000000000001 1.5 1.6 1.7000000000000002 1.8 1.9000000000000001 2.0 2.1 2.2 2.3000000000000003 2.4000000000000004 2.5 2.6 2.7 2.8000000000000003 2.9000000000000004 3.0 3.1 3.2 3.3000000000000003 3.4000000000000004 3.5 3.6 3.7 3.8000000000000003 3.9000000000000004 4.0 4.1000000000000005 4.2 4.3 4.4 4.5 4.6000000000000005 4.7 4.800000000000001 4.9 5.0 5.1000000000000005 5.2 5.300000000000001 5.4 5.5 5.6000000000000005 5.7 5.800000000000001 5.9 6.0 6.1000000000000005 6.2 6.300000000000001 6.4 6.5 6.6000000000000005 6.7 6.800000000000001 6.9 7.0 7.1000000000000005 7.2 7.300000000000001 7.4 7.5 7.6000000000000005 7.7 7.800000000000001 7.9 8.0 8.1 8.200000000000001 8.3 8.4 8.5 8.6 8.700000000000001 8.8 8.9 9.0 9.1 9.200000000000001 9.3 9.4 9.5 9.600000000000001 9.700000000000001 9.8 9.9)&lt;/p&gt;</description>
                <environment></environment>
            <key id="16019">CLJ-1166</key>
            <summary>Range function accumulates minor errors when called on floating-point numbers</summary>
                <type id="1" iconUrl="http://dev.clojure.org/jira/images/icons/bug.gif">Defect</type>
                                <priority id="5" iconUrl="http://dev.clojure.org/jira/images/icons/priority_trivial.gif">Trivial</priority>
                    <status id="6" iconUrl="http://dev.clojure.org/jira/images/icons/status_closed.gif">Closed</status>
                    <resolution id="2">Declined</resolution>
                                <assignee username="-1">Unassigned</assignee>
                                <reporter username="sfnelson">Stephen Nelson</reporter>
                        <labels>
                    </labels>
                <created>Tue, 19 Feb 2013 15:04:14 -0600</created>
                <updated>Fri, 29 Mar 2013 17:10:49 -0500</updated>
                    <resolved>Fri, 1 Mar 2013 10:08:43 -0600</resolved>
                            <version>Release 1.5</version>
                                                        <due></due>
                    <votes>0</votes>
                        <watches>0</watches>
                        <comments>
                    <comment id="30619" author="sfnelson" created="Tue, 19 Feb 2013 15:06:48 -0600"  >&lt;p&gt;=&amp;gt; (last (range 0.0 10000000.0 0.1))&lt;br/&gt;
9999999.98112945&lt;br/&gt;
=&amp;gt; (last (range&apos; 0.0 10000000.0 0.1))&lt;br/&gt;
9999999.9&lt;/p&gt;</comment>
                    <comment id="30674" author="stu" created="Fri, 1 Mar 2013 10:08:35 -0600"  >&lt;p&gt;Range is incremental by design, and that is how floats work.  Something with the desired behavior would need to be a different fn with a different name.&lt;/p&gt;</comment>
                    <comment id="30691" author="sfnelson" created="Sun, 3 Mar 2013 14:38:21 -0600"  >&lt;p&gt;&quot;Returns a lazy seq of nums from start (inclusive) to end (exclusive), by step&quot;&lt;/p&gt;

&lt;p&gt;What specifically about that wording specifically suggests that the implementation will use naive increment-and-recurse behaviour? My reading is that the function will return a lazy sequence of numbers from start to end separated by step, not separated by &apos;almost step&apos;.&lt;/p&gt;

&lt;p&gt;This implementation leads to unexpected behaviour with bounds:&lt;/p&gt;

&lt;p&gt;=&amp;gt; (count (range 0 100 1))&lt;br/&gt;
100&lt;br/&gt;
=&amp;gt; (count (range 0 10 0.1))&lt;br/&gt;
101&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.ima.umn.edu/~arnold/455.f96/disasters.html&quot;&gt;http://www.ima.umn.edu/~arnold/455.f96/disasters.html&lt;/a&gt;&lt;/p&gt;</comment>
                    <comment id="30845" author="timothypratley" created="Fri, 29 Mar 2013 17:09:50 -0500"  >&lt;p&gt;range could avoid this issue cleanly by converting floats to bigdecimals (let me know if you think this is a good idea?)&lt;/p&gt;

&lt;p&gt;I ran into this problem recently, and have to say it was pretty ugly. This is how I avoided the issue:&lt;/p&gt;

&lt;p&gt;(defn rangef&lt;br/&gt;
  &quot;Returns a sequence of n numbers from start to end inclusive.&quot;&lt;br/&gt;
  &lt;span class=&quot;error&quot;&gt;&amp;#91;start end n&amp;#93;&lt;/span&gt;&lt;br/&gt;
  (for &lt;span class=&quot;error&quot;&gt;&amp;#91;i (range 0 n)&amp;#93;&lt;/span&gt;&lt;br/&gt;
    (+ start (* i (/ (- end start) (dec n))))))&lt;/p&gt;

&lt;p&gt;Hope that helps any disillusioned float users out there, or just pass in BigDecimals to range instead of floats... I would go so far as to say using floats with range as it stands is almost always going to end in tears (or worse as Stephen describes &lt;img class=&quot;emoticon&quot; src=&quot;http://dev.clojure.org/jira/images/icons/emoticons/smile.gif&quot; height=&quot;20&quot; width=&quot;20&quot; align=&quot;absmiddle&quot; alt=&quot;&quot; border=&quot;0&quot;/&gt;).&lt;/p&gt;</comment>
                    <comment id="30846" author="timothypratley" created="Fri, 29 Mar 2013 17:10:49 -0500"  >&lt;p&gt;&lt;span class=&quot;error&quot;&gt;&amp;#91;and just to be clear if it is considered an error, it would be nice if range explicitly forbade it&amp;#93;&lt;/span&gt;&lt;/p&gt;</comment>
                </comments>
                    <attachments>
                </attachments>
            <subtasks>
        </subtasks>
                <customfields>
                                                                                            <customfield id="customfield_10010" key="com.pyxis.greenhopper.jira:gh-global-rank">
                <customfieldname>Global Rank</customfieldname>
                <customfieldvalues>
                    
                </customfieldvalues>
            </customfield>
                                                                                                            </customfields>
    </item>
</channel>
</rss>