www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7646] New: bug in code sample and unittest

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7646

           Summary: bug in code sample and unittest
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: trivial
          Priority: P2
         Component: websites
        AssignedTo: nobody puremagic.com
        ReportedBy: josvanuden gmail.com



The following sample code on std.functional contains a bug.
http://dlang.org/phobos/std_functional.html#memoize

    ulong fib(ulong n) { 
        alias memoize!fib mfib; 
        return n < 2 ? 1 : mfib(n - 2) + mfib(n - 1); 
    } 

    assert(fib(10) == 89);

fib(10) should be 55. 
http://en.wikipedia.org/wiki/Fibonacci_number#List_of_Fibonacci_numbers

I think the code should be (n <= 2):

    ulong fib(ulong n) { 
        alias memoize!fib mfib; 
        return n <= 2 ? 1 : mfib(n - 2) + mfib(n - 1); 
    }

    assert(fib(10) == 55); 

It's also in the unittest
https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 04 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7646





 I think the code should be (n <= 2):
 
     ulong fib(ulong n) { 
         alias memoize!fib mfib; 
         return n <= 2 ? 1 : mfib(n - 2) + mfib(n - 1); 
     }
 
     assert(fib(10) == 55); 
No, that still won't do. This is better: ulong fib(ulong n) { alias memoize!fib mfib; return n <= 2 ? n != 0 : mfib(n - 2) + mfib(n - 1); } -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7646


josvanuden gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P5


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7646




ulong fib (ulong n) { 
    alias memoize!fib mfib; 
    return n < 2 ? n : mfib(n - 2) + mfib(n - 1); 
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 11 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7646


josvanuden gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



It turns out some people regard F0 = 0 and some F0 = 1. So it's not a bug after
all.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 04 2012