Repository navigation
Fix integer power opcodes for negative exponents - #576
Open
raashish1601 wants to merge 1 commit into
Open
raashish1601 wants to merge 1 commit into
raashish1601 wants to merge 1 commit into
Conversation
For a negative exponent the int and long long power opcodes computed 1 / base. That divides by zero for a zero base (undefined behaviour, which crashes the process with MSVC builds) and ignores the exponent, so (-1) ** -2 gave -1. Return base for |base| == 1 and odd exponents, 1 for |base| == 1 and even exponents, and 0 otherwise.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For an integer
base ** nwherencomes from an array and is negative, the int and long long power opcodes compute1 / base. Two problems with that:0 ** -1divides by zero. That's undefined behaviour in C. With the Windows wheel (numexpr 2.14.2 from PyPI) it kills the process withSTATUS_INTEGER_DIVIDE_BY_ZERO; GCC happens to compile1 / xwithout a division, so Linux builds return 0.(-1) ** -2gives-1instead of1.With this change a negative exponent gives
baseforbase == ±1and an odd exponent,1forbase == ±1and an even exponent, and0otherwise (including0 ** -n, which keeps the result Linux builds already return, similar to integer division by zero giving 0). The scalar-exponent case is unchanged, sincepow_op()already rejects negative integer constants.Added
test_negative_power_arrayfor int32 and int64; it fails on main and passes with this change. Built the extension in apython:3.12Docker image and rannumexpr.tests.test_numexpr(6043 passed). Also added a line to the release notes.