summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2018-10-27 15:05:32 +0200
committerGitHub <noreply@github.com>2018-10-27 15:05:32 +0200
commit5fdbfe49d56d756169e75c80c126eee5fc8fad52 (patch)
treec2e409195041dbf3cc44ac7c6493dbf7ddbbdd25
parentc2de8efb67f80bff59975641aac387d652324e4e (diff)
parent768b7489fed1ea1e347c346493d0f3c8a6187bab (diff)
downloadcython-5fdbfe49d56d756169e75c80c126eee5fc8fad52.tar.gz
Merge pull request #2683 from KPilnacek/2133_exponentiation_of_int
Fix: Exponentiation of integer constants
-rw-r--r--Cython/Compiler/ExprNodes.py6
-rw-r--r--docs/src/tutorial/caveats.rst1
-rw-r--r--tests/run/constant_folding.py11
3 files changed, 17 insertions, 1 deletions
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py
index c2558dcfa..2c7e025b8 100644
--- a/Cython/Compiler/ExprNodes.py
+++ b/Cython/Compiler/ExprNodes.py
@@ -11745,6 +11745,12 @@ class PowNode(NumBinopNode):
error(self.pos, "got unexpected types for C power operator: %s, %s" %
(self.operand1.type, self.operand2.type))
+ def compute_c_result_type(self, type1, type2):
+ c_result_type = super(PowNode, self).compute_c_result_type(type1, type2)
+ if isinstance(self.operand2.constant_result, _py_int_types) and self.operand2.constant_result < 0:
+ c_result_type = PyrexTypes.widest_numeric_type(c_result_type, PyrexTypes.c_double_type)
+ return c_result_type
+
def calculate_result_code(self):
# Work around MSVC overloading ambiguity.
def typecast(operand):
diff --git a/docs/src/tutorial/caveats.rst b/docs/src/tutorial/caveats.rst
index 65443ae26..192313162 100644
--- a/docs/src/tutorial/caveats.rst
+++ b/docs/src/tutorial/caveats.rst
@@ -5,7 +5,6 @@ Since Cython mixes C and Python semantics, some things may be a bit
surprising or unintuitive. Work always goes on to make Cython more natural
for Python users, so this list may change in the future.
- - ``10**-2 == 0``, instead of ``0.01`` like in Python.
- Given two typed ``int`` variables ``a`` and ``b``, ``a % b`` has the
same sign as the second argument (following Python semantics) rather than
having the same sign as the first (as in C). The C behavior can be
diff --git a/tests/run/constant_folding.py b/tests/run/constant_folding.py
index 2df8da308..4dd70a625 100644
--- a/tests/run/constant_folding.py
+++ b/tests/run/constant_folding.py
@@ -136,6 +136,17 @@ def binop_mul_pow():
return (mul_int, mul_large_int, pow_int, pow_large_int)
+def binop_pow_negative():
+ """
+ >>> print_big_ints(binop_pow_negative())
+ (4.018775720164609e-06, 8.020807320287816e-38, 0.1)
+ """
+ pow_int = 12 ** -5
+ pow_large_int = 1234 ** -12
+ pow_expression_int = 10 ** (1-2)
+ return (pow_int, pow_large_int, pow_expression_int)
+
+
@cython.test_fail_if_path_exists(
"//SliceIndexNode",
)