# Fib.rb 1.9.1/1.8.6 source code conflicts/errors

**URL:** <https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376>\
**Category:** Ruby\
**Created:** [January 11, 2010, 6:31am UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376 "2010-01-11T06:31:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [January 11, 2010, 6:31am UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376/1 "2010-01-11T06:31:13Z")

</div>

In the latest ruby-1.8.6-p388 source code I just downloaded  
the file fib.rb; 2007-2-12 contains correct code:

* * *

# calculate Fibonacci(20)

# benchmark

## def fib(n) if n\<2 n else fib(n-2)+fib(n-1) end end print(fib(20), “\n”)

The code in fib.(awk,pl,py.scm) is also correct.

But in the lastest ruby-1.9.1-p378 source code

the file fib.rb (and fib.(awk,pl,py,scm) files)

has the same date but the code has been changed to  
give an incorrect value for fib(0), which should be 0:

* * *

def fib n  
if n \< 3  
1  
else  
fib(n-1) + fib(n-2)  
end  
end

## fib(34)

The correct series starts with ‘0’ and is::

## n | 0 1 2 3 4 5 6 7 8 9 10…

fib(n) | 0 1 1 2 3 5 8 13 21 34 55,…

See for verification:  
[http://goldennumber.net/fibonser.htm](http://goldennumber.net/fibonser.htm)

> **[Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_number)**
>
> In mathematics, the Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones. Numbers that are part of the Fibonacci sequence are known as Fibonacci numbers, commonly denoted Fn . The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the first few values in the sequence are:
> The Fibonacci numbers were first described in Indian mathematics, as early as ...

This is troubling, because not only does the 1.9.1 code  
not give the fully accurate results, but the changes to  
the file doesn’t show a newer/different data than the  
file in the 1.8.6 source code. The dates are the same.

Is someone messing with the source code, or has the 1.9.1  
line not been thoroughly tested/vetted to catch these  
obvious errors/differences from the prior versions?

A BIG obvious question now is, what else has been changed  
that no longer completely gives accurate answers/results?

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [January 11, 2010, 12:06pm UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376/2 "2010-01-11T12:06:24Z")

</div>

jzakiya wrote:

> The code in fib.(awk,pl,py.scm) is also correct.
> 
> But in the lastest ruby-1.9.1-p378 source code
> 
> the file fib.rb (and fib.(awk,pl,py,scm) files)
> 
> has the same date but the code has been changed to  
> give an incorrect value for fib(0), which should be 0:

So you have two complaints:

(1) The files are different between two different versions of ruby (1.8  
and 1.9), but have the same timestamp in the respective tarfiles.

(2) The new files are computing fib(0) “incorrectly”.

Actually, you’re not entirely right in case (1). I see that:

- ruby-1.8.6-p388/sample/fib.rb and ruby-1.9.1-p378/sample/fib.rb are  
identical

$ diff -u ruby-1.8.6-p388/sample/fib.rb ruby-1.9.1-p378/sample/fib.rb  
$

- ruby-1.9.1-p378 contains benchmark/other-lang/fib.rb (with slightly  
different code), but it does not exist in ruby-1.8.6-p388 at all.

Regarding your point (2): the code you are referring to is in the  
benchmark/ directory, so I don’t think the return value is important.  
After all, the code for the other languages is doing exactly the same.

The “correct” version of the code is still in the sample/ directory.

Looking at ‘svn log’, the benchmark directory found its way into trunk  
when YARV was merged in. There’s no explanation of why the code used for  
benchmarking was slightly different to the fib sample code.

You can always raise a ticket on [redmine.ruby-lang.org](http://redmine.ruby-lang.org) if you are  
unhappy about the algorithms used in the benchmarks, but it seems pretty  
trivial to me.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [January 11, 2010, 9:25pm UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376/3 "2010-01-11T21:25:42Z")

</div>

> Regarding your point (2): the code you are referring to is in the  
> benchmark/ directory, so I don’t think the return value is important.  
> After all, the code for the other languages is doing exactly the same.
> 
> The “correct” version of the code is still in the sample/ directory.
> 
> Looking at ‘svn log’, the benchmark directory found its way into trunk  
> when YARV was merged in. There’s no explanation of why the code used for  
> benchmarking was slightly different to the fib sample code.

Thanks for tracking down the apparent source for this code.

> You can always raise a ticket on [redmine.ruby-lang.org](http://redmine.ruby-lang.org) if you are  
> unhappy about the algorithms used in the benchmarks, but it seems pretty  
> trivial to me.

The ‘solution’ to this ‘problem’ is to change the erroneous coding to  
a correct  
one, or delete the file(s) (all the other incorrect language versions  
too).

This is not a ‘trivial’ matter. It is a matter of mindset that is  
troubling to me.

The Fibonacci series has been known, taught, and used for centuries.  
It is not  
a matter of speculation or ambiguities. Either you write an algorithm  
to do it  
correctly, or you are writing an algorithm to do some other series.

This lack of an apparent need to satisfy mathematical rigor is  
troubling, and  
potentially dangerous. You don’t know who, and for what purpose,  
someone will use  
Ruby for, so it needs to be as well written, and accurate, as  
possible.

I use Ruby in engineering, scientific, mathematical, and educational  
fields. So  
doing math to me matters. Doing it correctly matters. Other people use  
Ruby in  
these fields too, so it matters to them as well. See this one example  
below:

[http://www.artcompsci.org/kali/five-year-plan3.html](http://www.artcompsci.org/kali/five-year-plan3.html)

That’s why these releases came out, to ‘fix’ a mistake in the coding  
that allowed security related exploits to occur. Now, someone ‘could’  
say these mistakes are unlikely to cause any real harm, that they’re  
‘trivial’, but you don’t allow these errors to exist when you find  
them. You ‘fix’ them, and keep looking for others.

So, since we agree the code in question is ‘wrong’, it should be  
corrected and/or discarded.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [January 11, 2010, 10:45pm UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376/4 "2010-01-11T22:45:52Z")

</div>

> No, I disagree that the code is ‘wrong’ (that’s why I used the quotes).

Yes, the code is ‘wrong’ because it does not correctly produce the  
Fibonacci series.

> It’s a benchmark. Its sole purpose in life is to be run a number of  
> times, and to have its execution time measured. If fib() were renamed to  
> foo() or bignum\_test() would that make you happy?

Actually, yes it would make me happy to rename. It would make me more  
happy to completely delete it, and not confuse anyone.

> Indeed, “f” “i” “b” are just three letters. Unless I’m mistaken, nowhere  
> is it claimed that this code calculates the correct value of the  
> Fibonacci series. The code does happen to do so for n\>0.

This is very troubling that you feel some ‘need’ to even make this  
specious argument. The code is ‘wrong’. Its doesn’t matter what its  
intimated purpose is. Anyone looking at this code, no matter what  
directory it’s in, will naturally assume it’ll produce (allegedly) the  
correct Fibonacci series. Like I said, you don’t know who is looking  
at this code and for what purpose they’ll put it to use. There was no  
disclaimer in this code saying: ‘Intended only for benchmarking, not  
for real use.’

For some reason, when ‘I’ identify an irrefutable coding ‘error’ of a  
mathematical algorithm it just not good enough to accept it, fix it,  
and move on.

Why defend inaccuracy and mediocrity?

Like I said, this is a troubling mindset.

---

<div class="post-metadata">

**Author:** ![jzakiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ec8ea/32.png) [@jzakiya](https://www.ruby-forum.com/u/jzakiya)\
**Post date:** [January 11, 2010, 9:46pm UTC](https://www.ruby-forum.com/t/fib-rb-1-9-1-1-8-6-source-code-conflicts-errors/181376/5 "2010-01-11T21:46:25Z")

</div>

jzakiya wrote:

> So, since we agree the code in question is ‘wrong’, it should be  
> corrected and/or discarded.

No, I disagree that the code is ‘wrong’ (that’s why I used the quotes).

It’s a benchmark. Its sole purpose in life is to be run a number of  
times, and to have its execution time measured. If fib() were renamed to  
foo() or bignum\_test() would that make you happy?

Indeed, “f” “i” “b” are just three letters. Unless I’m mistaken, nowhere  
is it claimed that this code calculates the correct value of the  
Fibonacci series. The code does happen to do so for n\>0.

Anyway, I’m not inclined to argue further. Redmine or ruby-core would be  
the appropriate places to post patches.
