google / WebFundamentals

Former git repo for WebFundamentals on developers.google.com
Apache License 2.0
13.85k stars 2.57k forks source link

Fibonacci formula is incorrect #7634

Open mhewett-ks opened 5 years ago

mhewett-ks commented 5 years ago

Welcome! Please use this template for reporting documentation issues on https://developers.google.com/web/.

For questions about Chrome DevTools, checkout the google-chrome-devtools tag on StackOverflow. For questions about Chrome, check the google-chrome tag. If you think you've found a Chrome bug, check out our feedback page for more details on how to file it.

Page Affected: https://developers.google.com/web/TODO

What needs to be done?

The web page https://developers.google.com/web/updates/2018/03/emscripting-a-c-library is an excellent example of using Emscripten but the example Fibonacci program on the page is incorrect. Instead of returning Fib(n), it returns Fib(n+1). For example, Fib(5) = 5, but the supplied function returns 8. See http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html for an authoritative list of Fibonacci numbers.

This does not affect the value of the page, but is disconcerting to math-ocd people :-).

The problem can be fixed in many ways, but I fixed it like this:

include

EMSCRIPTEN_KEEPALIVE int fib(int n) { int i, t, a = 0, b = 1;

if (n <= 0) { return 0; }

for (i = 1; i < n; i++) { t = a + b; a = b; b = t; } return b; }

mfurkan60 commented 5 years ago

maybe you can use recursion method by like this.