Textbook example - Result is off on Linux with distutils and cythonize

36 переглядів
Перейти до першого непрочитаного повідомлення

Vraj Mohan

не прочитано,
15 серп. 2016 р., 04:11:0715.08.16
Кому: cython-users
I am new to Cython.

When I tried the Fibonacci example from Kurt Smith's book https://library.oreilly.com/book/0636920033431/cython/toc.xhtml, I found the result is off for fib(37).

Here's the Cython code - fib.pyx:

def fib(int n):
    cdef int i
    cdef double a=0.0, b=1.0
    for i in range(n):
        a, b = a + b, a
    return a

Here's test code - test_fib.py:

from fib import fib

for i in range(35, 38):
     print(fib(i))
assert fib(35) + fib(36) == fib(37)

The output is:
9227465.0
14930352.0
24157816.0
Traceback (most recent call last):
  File "test_fib.py", line 7, in <module>
    assert fib(35) + fib(36) == fib(37)
AssertionError


This issue happens with:
1. distutils and cythonize
2. pyximport

This works correctly with:
1. Interactive Cython with IPython’s %%cython Magic
2. On OS X (using distutils and cythonize)


Jeroen Demeyer

не прочитано,
15 серп. 2016 р., 16:14:0315.08.16
Кому: cython...@googlegroups.com
On 2016-08-14 19:05, Vraj Mohan wrote:
> I am new to Cython.
>
> When I tried the Fibonacci example from Kurt Smith's book
> https://library.oreilly.com/book/0636920033431/cython/toc.xhtml, I found
> the result is off for fib(37).

Works for me. No idea what could be wrong. Maybe a broken C compiler? A
hardware problem?

Robert Bradshaw

не прочитано,
15 серп. 2016 р., 21:25:0215.08.16
Кому: cython...@googlegroups.com
I can't reproduce this either. Could you give exact details on the
failing system? Do you have a strange setup of some sort? Could you
print a and b in the loop?

Something's really messed up if your CPU gives 24157816.0 for
9227465.0 + 14930352.0.

Jeroen Demeyer

не прочитано,
16 серп. 2016 р., 03:28:2716.08.16
Кому: cython...@googlegroups.com
Here is a guess: the output is what you get when using single-precision
floats (they have 24 bits of precision, so 24157817.0 gets rounded to
24157816.0). So it seems that something (most likely some library) has
put your CPU in single-precision mode.

Vraj Mohan

не прочитано,
16 серп. 2016 р., 08:25:5916.08.16
Кому: cython-users
The example works correctly on a different system with the identical Linux dist - Ubuntu 16.04.1.

The build environment on my original system must be borked.

Thank you for investigating this, and sorry to waste your time.

--Vraj
Відповісти всім
Відповісти автору
Переслати
0 нових повідомлень