<div dir="ltr"><div><div><div>The competition does it better, see Python's fsum:<br><br>import math<br><br>janus = [31.0, 2e+34, -1.2345678901235e+80, 2749.0, -2939234.0, -2e+33,<br>         3.2e+270, 17.0, -2.4e+270, 4.2344294738446e+170, 1.0, -8e+269, 0.0, 99.0]<br>
<br>print(math.fsum(janus))<br># 4.2344294738446e+170<br><br></div>The fsum algorithm is interesting, it essentially emulates unlimited-precision FP <br></div>by using a list of limited-precision FP numbers.<br><br></div>
Stephan<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/11/7 Todd O'Bryan <span dir="ltr"><<a href="mailto:toddobryan@gmail.com" target="_blank">toddobryan@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I just found a lovely Java expression to emphasize the inexactness of<br>
doubles to my AP students. The problem--which I think is from<br>
HtDP/1e--is to find the value of a bag of coins given the number of<br>
pennies, nickels, dimes, and quarters. In BlueJ's code pad (or similar<br>
in DrJava, jGrasp, etc.)<br>
<br>
> 0.01 + 0.05 + 0.10 + 0.25<br>
0.410000000000000000003<br>
<br>
(my number of zeroes may be off)<br>
<br>
As one of my students said--"You can do that in your head. What's the<br>
computer's problem?"<br>
<span class="HOEnZb"><font color="#888888"><br>
Todd<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
On Wed, Nov 6, 2013 at 1:30 PM, Neil Toronto <<a href="mailto:neil.toronto@gmail.com">neil.toronto@gmail.com</a>> wrote:<br>
> On 11/06/2013 09:24 AM, Matthias Felleisen wrote:<br>
>><br>
>><br>
>> On Nov 6, 2013, at 7:13 AM, Ben Duan <<a href="mailto:yfefyf@gmail.com">yfefyf@gmail.com</a>> wrote:<br>
>><br>
>>> Thank you, Jens. I didn't know that the inexactness of floating point<br>
>>> numbers could make such a big difference.<br>
>><br>
>><br>
>><br>
>>  From HtDP/1e:<br>
>><br>
>> (define JANUS<br>
>>    (list #i31<br>
>>          #i2e+34<br>
>>          #i-1.2345678901235e+80<br>
>>          #i2749<br>
>>          #i-2939234<br>
>>          #i-2e+33<br>
>>          #i3.2e+270<br>
>>          #i17<br>
>>          #i-2.4e+270<br>
>>          #i4.2344294738446e+170<br>
>>          #i1<br>
>>          #i-8e+269<br>
>>          #i0<br>
>>          #i99))<br>
>><br>
>><br>
>><br>
>> ;; [List-of Number] -> Number<br>
>> ;; add numbers from left to right<br>
>> (check-expect (sumlr '(1 2 3)) 6)<br>
>> (define (sumlr l)<br>
>>    (foldl + 0 l))<br>
>><br>
>> ;; [List-of Number] -> Number<br>
>> ;; add numbers from right to left<br>
>> (check-expect (sumrl '(1 2 3)) 6)<br>
>> (define (sumrl l) (foldr + 0 l))<br>
>><br>
>> Then apply the two functions to JANUS. Enjoy -- Matthias<br>
><br>
><br>
> Nice example!<br>
><br>
> You could also (require math) and apply its `sum' or `flsum' to JANUS. Then<br>
> *really* enjoy. :D<br>
><br>
>> (sumlr JANUS)<br>
> 99.0<br>
><br>
>> (sumrl JANUS)<br>
> -1.2345678901235e+80<br>
><br>
>> (sum JANUS)<br>
> 4.2344294738446e+170<br>
><br>
>> (exact->inexact (sumlr (map inexact->exact JANUS)))<br>
> 4.2344294738446e+170<br>
><br>
> On my computer, using `sum' is about 20x faster than converting JANUS to<br>
> exact numbers.<br>
><br>
> You can also sort by absolute value before summing, which is a little faster<br>
> still but loses some precision. Do not trust Teh Internets on this one.<br>
> Popular Q-and-A sites say to sort ascending, which makes intuitive sense:<br>
> adding a big number to two small numbers in turn might do nothing, but<br>
> adding a big number to their *sum* might result in something larger.<br>
><br>
>> (expt 2 53.0)<br>
> 9007199254740992.0<br>
><br>
>> (sumlr (list (expt 2 53.0) 1.0 1.0))<br>
> 9007199254740992.0<br>
><br>
>> (sumlr (list 1.0 1.0 (expt 2 53.0)))<br>
> 9007199254740994.0<br>
><br>
> But JANUS shows that sorting ascending doesn't work when summing huge<br>
> numbers with alternating signs:<br>
><br>
>> (sumlr (sort JANUS (λ (x y) (< (abs x) (abs y)))))<br>
> 0.0<br>
><br>
>> (sumlr (sort JANUS (λ (x y) (> (abs x) (abs y)))))<br>
> 4.2344294738446e+170<br>
><br>
> All the research papers on summation by sorting sort descending, contrary to<br>
> the wisdom of Teh Internets. So either do that, or use `sum' or `flsum' when<br>
> you want an accurate sum of flonums.<br>
><br>
> Neil ⊥<br>
><br>
><br>
> ____________________<br>
>  Racket Users list:<br>
>  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br>
____________________<br>
  Racket Users list:<br>
  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
</div></div></blockquote></div><br></div>