A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/cython/cython/wiki/enhancements-division below:

enhancements division · cython/cython Wiki · GitHub

CEP 516 - Division Semantics

Python and C have different semantics for division with signed integers. Python rounds towards infinity while C (in the C99 standard, and by convention before that) rounds towards 0. Thus -1 % 5 yields 4 in Python and -1 in C. The question is how should we handle this in Cython for cdef integers (including literals). The current implementation follows C, this proposal suggests changing the semantics to follow Python.

Both the division and modulo operator should be affected, as we wish to maintain (a//b) * b + (a % b) == a.

We also will test for division by zero, raising a ZeroDivisionError in that case rather than aborting.

Obtaining the original behavior

Because of speed considerations, existing code, and people who prefer or need the original semantics, it is imperative we provide a way to get the original behavior. Some alternatives:

Final Decision and Status

This CEP will go into place in Cython 0.12, with a warning beforehand.

A compiler directive cython.cdivision and two special cython.cdiv/cmod are implemented. Runtime warnings can be enabled with the compiler directive cython.cdivision_warnings.

Python division seems to incur a 30-40% speed regression (timings below on OS X intel core duo, but should be representative of x86 at least).

attachment:time_mod.pyx

sage: import time_mod
sage: time time_mod.mod_c(2, 11, 10^8)
CPU times: user 1.68 s, sys: 0.01 s, total: 1.69 s
Wall time: 1.73 s
0
sage: time time_mod.mod_py(2, 11, 10^8)
CPU times: user 2.31 s, sys: 0.01 s, total: 2.31 s
Wall time: 2.33 s
11
sage: 2.33 / 1.73
1.34682080924855

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4