|
Download cpython_14/instruction.md from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 12.2 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_14/instruction.md
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/cpython_14/instruction.md
-
curl -L -o instruction.md https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/cpython_14/instruction.md
12.2 kB
| You are working in `/workspace/cpython`, a source tree checked out at | |
| the base commit for this task. Implement the requested behavior in the source | |
| tree, then run: | |
| ```bash | |
| lolbench-submit | |
| ``` | |
| Do not stop after editing files, running tests, or describing the solution. The | |
| task is complete only when `lolbench-submit` has created | |
| `/logs/artifacts/solution.patch`. If that file does not exist, continue | |
| working and run `lolbench-submit` again. | |
| That command writes your implementation diff to | |
| `/logs/artifacts/solution.patch`, which is the artifact the Harbor verifier | |
| will grade. Before running `lolbench-submit`, clean or revert any test files | |
| you created or modified; test files must not be included in the final | |
| `solution.patch`. | |
| This environment has no outbound internet access — `curl`/`wget`, `git fetch`/`clone`, package installs, and web fetch/search will all fail. Implement the requirements using only the code already in the workspace and your own knowledge; do not attempt to fetch or search external resources. | |
| Now please implement the following requirements in the source tree: | |
| --- | |
| # PEP 791: math.integer --- submodule for integer-specific mathematics functions | |
| ## Abstract | |
| This PEP proposes a new submodule for number-theoretical, combinatorial and | |
| other functions defined for integer arguments, like | |
| `math.gcd` or `math.isqrt`. | |
| ## Motivation | |
| The `math` documentation says: "This module provides | |
| access to the mathematical functions defined by the C standard." But, over | |
| time the module was populated with functions that aren't related to the C | |
| standard or floating-point arithmetics. Now it's much harder to describe | |
| module scope, content and interfaces (returned values or accepted arguments). | |
| For example, the following statement from the documentation: "Except when | |
| explicitly noted otherwise, all return values are floats." This is no longer | |
| true: *None* of the functions listed in the [Number-theoretic functions](https://docs.python.org/3.14/library/math.html#number-theoretic-functions) | |
| subsection of the documentation return a float, but the documentation doesn't | |
| say so. In the documentation for the proposed `math.integer` submodule the sentence | |
| "All return values are integers" would be accurate. In a similar way we can | |
| simplify the description of the accepted arguments for functions in both the | |
| new submodule and in `math`. | |
| Now it's a lot harder to satisfy people's expectations about the module | |
| content. For example, should they expect that `math.factorial(100)` will | |
| return an exact answer? Many languages, Python packages (like `scipy`) | |
| or pocket calculators have functions with same or similar name, that return a | |
| floating-point value, which is only an approximation in this example. | |
| Apparently, the `math` module can't serve as a catch-all | |
| place for mathematical functions since we also have the | |
| `cmath` and `statistics` modules. | |
| Let's do the same for integer-related functions. It provides shared context, | |
| which reduces verbosity in the documentation and conceptual load. It also aids | |
| discoverability through grouping related functions and makes IDE (e.g. new | |
| CPython's REPL) suggestions more helpful. | |
| Currently the `math` module code in the CPython is around | |
| 4200LOC, from which the new module code is roughly 1/3 (1300LOC). This is | |
| comparable with the `cmath` (1340LOC), which is *not* a | |
| simple wrapper to the `libm`, as most functions in the | |
| `math` module. | |
| And this situation tends to get worse. When the module split [was first proposed](https://mail.python.org/archives/list/[email-redacted]/thread/YYJ5YJBJNCVXQWK5K3WSVNMPUSV56LOR/), | |
| there were only two integer-related functions: | |
| `~math.factorial` (accepting also `float`'s, like | |
| other functions in the module) and `~math.gcd` (moved | |
| from the `fractions` module). Then | |
| `~math.isqrt`, `~math.comb` and | |
| `~math.perm` were added, and addition of the new module | |
| was [proposed second time](https://github.com/python/cpython/issues/81313), | |
| so all new functions would go directly to it, without littering the | |
| `math` namespace. Now there are six functions and | |
| `~math.factorial` doesn't accept `float`\ s | |
| anymore. | |
| Some possible additions, among those proposed in the initial discussion thread | |
| and issue [python/cpython#81313](https://github.com/python/cpython/issues/81313) are: | |
| * `c_div()` and `n_div()` --- for integer division with rounding towards | |
| positive infinity (ceiling divide) and to the nearest integer, see [relevant discussion thread](https://discuss.python.org/t/91269). This is reinvented | |
| several times in the stdlib, e.g. in `datetime` and `fractions`. | |
| And it's easy to do this wrongly, as demonstrated by the thread. | |
| * `gcdext()` --- to solve linear [Diophantine equation](https://en.wikipedia.org/wiki/Diophantine_equation) in two variables (the | |
| `int` implementation actually includes an extended | |
| Euclidean algorithm) | |
| * `isqrt_rem()` --- to return both an integer square root and a remainder | |
| (which is non-zero only if the integer isn't a perfect square) | |
| * `ilog()` --- integer logarithm, `math.log` has | |
| special handling for integer arguments. It's unique (with respect to other | |
| module functions) and not documented so far, see issue [python/cpython#120950](https://github.com/python/cpython/issues/120950). | |
| * `fibonacci()` --- [Fibonacci sequence](https://en.wikipedia.org/wiki/Fibonacci_sequence). | |
| Separated namespace eliminates possible name clash with existing | |
| `math`'s module functions. For example, possible names | |
| `ceil_div()` or `ceildiv()` for integer ceiling division will interfere | |
| with the `~math.ceil` (which is for `float`'s and | |
| *sometimes* does right things for integer division, as an accident --- but | |
| [usually not](https://discuss.python.org/t/91269/6)). | |
| ## Rationale | |
| Is this all about documentation, why not fix it instead? No, it isn't. Sure, | |
| we can be much more vague in the module preamble (i.e. roughly say that "the | |
| `math` module contains some mathematical functions"), we | |
| can accurately describe input/output for each function and its behavior (e.g. | |
| whether the `~math.factorial` output is exact or not, | |
| like the [scipy.special.factorial](https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.factorial.html#scipy.special.factorial), | |
| per default). | |
| But the major issue is that the current module mixes different, almost | |
| non-interlaced application domains. Adding more documentation will just | |
| highlight this and make the issue worse for end users (more text to read/skip). | |
| And it will not fix the issue with discoverability (to know in which module to find | |
| a function, and that it can be found at all, you need to look at all the | |
| functions in the module), nor with tab-completion. | |
| ## Specification | |
| The PEP proposes moving the following integer-related functions to a new | |
| submodule, called `math.integer`: | |
| * `~math.comb` | |
| * `~math.factorial` | |
| * `~math.gcd` | |
| * `~math.isqrt` | |
| * `~math.lcm` | |
| * `~math.perm` | |
| Their aliases in `math` will be `soft deprecated`. | |
| This PEP doesn't introduce backward-incompatible changes. | |
| Module functions will accept integers and objects that implement the | |
| `~object.__index__` method, which is used to convert the | |
| object to an integer number. Suitable functions must be computed exactly, | |
| given sufficient time and memory. | |
| The `intmath` package, available on PyPI, will provide new submodule | |
| content for older Python versions. | |
| ## Possible Extensions | |
| New functions (like mentioned in Motivation section) are not | |
| part of this proposal. | |
| Though, we should mention that, unless we can just provide bindings to some | |
| well supported mathematical library like the GMP, the submodule scope should be | |
| limited. For example, no primality testing and factorization, as | |
| production-quality implementatons will require a decent mathematical background | |
| from contributors and belongs rather to specialized libraries. | |
| When proposed function already exists in the `gmpy2`, we should prefer a | |
| compatible interface for the stdlib. | |
| ## Backwards Compatibility | |
| As aliases in `math` will be kept indefinitely (their use | |
| would be discouraged), there are no anticipated code breaks. | |
| ## How to Teach This | |
| The new submodule will be a place for functions, that 1) accept | |
| `int`-like arguments and also return integers, and 2) | |
| are also in the field of arbitrary-precision integer arithmetic, i.e. have no | |
| dependency on the platform floating-point format or behaviour and/or on the | |
| platform math library (`libm`). | |
| For users it would be natural first to look on the | |
| `int`'s methods, which cover most basic use-cases (e.g. | |
| `int.bit_length` method), than to some dedicated place in | |
| the stdlib. | |
| ## Reference Implementation | |
| [python/cpython#133909](https://github.com/python/cpython/pull/133909) | |
| ## Rejected ideas | |
| ### isqrt() renaming | |
| There was a brief discussion about exposing `math.isqrt` | |
| as `sqrt` in the new namespace in the same way that | |
| `cmath.sqrt` is the complex version of | |
| `math.sqrt`. However, `isqrt` is ultimately a | |
| different function: it is the floor of the square root. It would be confusing | |
| to give it the same name (under a different submodule). | |
| ### Module name | |
| [Polling showed](https://discuss.python.org/t/92548/67) `intmath` as most | |
| popular candidate with `imath` as a second winner. | |
| Other proposed names include `ntheory` (like SymPy's submodule), | |
| `integermath`, `zmath`, `dmath` and `imaths`. | |
| But the SC prefers a submodule rather than a new top-level module. Most | |
| popular variants of the `math`'s submodule are: | |
| `integer`, `discrete` or `ntheory`. | |
| ## Linked Issue #81313 — PEP-791: Add a module for integer related math functions | |
| BPO | [37132](https://bugs.python.org/issue37132) | |
| --- | :--- | |
| Nosy | @tim-one, @rhettinger, @mdickinson, @serhiy-storchaka | |
| PRs | <li>python/cpython#13741</li> | |
| <sup>*Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.*</sup> | |
| <details><summary>Show more details</summary><p> | |
| GitHub fields: | |
| ```python | |
| assignee = None | |
| closed_at = <Date 2019-06-03.07:13:59.683> | |
| created_at = <Date 2019-06-02.10:54:57.404> | |
| labels = ['3.8', 'type-feature', 'library'] | |
| title = 'Add a module for integer related math functions' | |
| updated_at = <Date 2020-05-07.16:27:37.033> | |
| user = 'https://github.com/serhiy-storchaka' | |
| ``` | |
| bugs.python.org fields: | |
| ```python | |
| activity = <Date 2020-05-07.16:27:37.033> | |
| actor = 'mark.dickinson' | |
| assignee = 'none' | |
| closed = True | |
| closed_date = <Date 2019-06-03.07:13:59.683> | |
| closer = 'serhiy.storchaka' | |
| components = ['Library (Lib)'] | |
| creation = <Date 2019-06-02.10:54:57.404> | |
| creator = 'serhiy.storchaka' | |
| dependencies = [] | |
| files = [] | |
| hgrepos = [] | |
| issue_num = 37132 | |
| keywords = ['patch'] | |
| message_count = 12.0 | |
| messages = ['344269', '344274', '344277', '344279', '344280', '344281', '344283', '344296', '344297', '344381', '344387', '368354'] | |
| nosy_count = 5.0 | |
| nosy_names = ['tim.peters', 'rhettinger', 'mark.dickinson', 'stutzbach', 'serhiy.storchaka'] | |
| pr_nums = ['13741'] | |
| priority = 'normal' | |
| resolution = 'rejected' | |
| stage = 'resolved' | |
| status = 'closed' | |
| superseder = None | |
| type = 'enhancement' | |
| url = 'https://bugs.python.org/issue37132' | |
| versions = ['Python 3.8'] | |
| ``` | |
| </p></details> | |
| See PEP-791 | |
| *Source: https://github.com/python/cpython/issues/81313* | |
| ## Linked Issue #120950 — math.log handles int but not 3rd party integer types | |
| # Bug report | |
| ### Bug description: | |
| The `math.log` function has a special path for integers that only works for the builtin `int` type: | |
| https://github.com/python/cpython/blob/ac61d58db0753a3b37de21dbc6e86b38f2a93f1b/Modules/mathmodule.c#L2220-L2223 | |
| That means that it does not work for 3rd party integer types e.g.: | |
| ```python | |
| In [9]: n = gmpy2.mpz(10)**1000 | |
| In [10]: math.log(int(n)) | |
| Out[10]: 2302.585092994045 | |
| In [12]: math.log(operator.index(n)) | |
| Out[12]: 2302.585092994045 | |
| # In [11]: math.log(n) | |
| OverflowError Traceback (most recent call last) | |
| Cell In[11], line 1 | |
| ----> 1 math.log(n) | |
| OverflowError: 'mpz' too large to convert to float | |
| ``` | |
| Maybe if there is a special integer handling path in `math.log` it should check for `__index__` before `__float__`. | |
| Related gh-106502 is about `math.log` with `Decimal`. | |
| ### CPython versions tested on: | |
| 3.13 | |
| ### Operating systems tested on: | |
| _No response_ | |
| *Source: https://github.com/python/cpython/issues/120950* | |