Implement mod for floats.
authorRobert Bradshaw <robertwb@math.washington.edu>
Sat, 27 Sep 2008 20:52:44 +0000 (13:52 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Sat, 27 Sep 2008 20:52:44 +0000 (13:52 -0700)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Symtab.py
tests/errors/e_modop.pyx [deleted file]
tests/run/fmod.pyx [new file with mode: 0644]

index 733124db6036346ffe227d2195442ab18acd8ed6..bcd29039a52fa9967ddb356130e5ad76f8586bd1 100644 (file)
@@ -3414,22 +3414,27 @@ class FloorDivNode(NumBinopNode):
             self.operand2.result())
 
 
-class ModNode(IntBinopNode):
+class ModNode(NumBinopNode):
     #  '%' operator.
     
     def is_py_operation(self):
         return (self.operand1.type.is_string
             or self.operand2.type.is_string
-            or IntBinopNode.is_py_operation(self))
+            or NumBinopNode.is_py_operation(self))
 
+    def calculate_result_code(self):
+        if self.operand1.type.is_float or self.operand2.type.is_float:
+            return "fmod(%s, %s)" % (
+                self.operand1.result(), 
+                self.operand2.result())
+        else:
+            return "(%s %% %s)" % (
+                self.operand1.result(), 
+                self.operand2.result())
 
 class PowNode(NumBinopNode):
     #  '**' operator.
 
-    def analyse_types(self, env):
-        env.pow_function_used = 1
-        NumBinopNode.analyse_types(self, env)
-
     def compute_c_result_type(self, type1, type2):
         if self.c_types_okay(type1, type2):
             return PyrexTypes.c_double_type
index 127d7c2487c731322c078edfe42951c846a883f7..35ce1c170c1d390cea2853db5d74266ea085bd3a 100644 (file)
@@ -167,7 +167,6 @@ class Scope:
     # temp_counter      integer            Counter for naming temp vars
     # cname_to_entry    {string : Entry}   Temp cname to entry mapping
     # int_to_entry      {int : Entry}      Temp cname to entry mapping
-    # pow_function_used boolean            The C pow() function is used
     # return_type       PyrexType or None  Return type of function owning scope
     # is_py_class_scope boolean            Is a Python class scope
     # is_c_class_scope  boolean            Is an extension type scope
@@ -221,7 +220,6 @@ class Scope:
         #self.pending_temp_entries = [] # TEMPORARY
         self.temp_counter = 1
         self.cname_to_entry = {}
-        self.pow_function_used = 0
         self.string_to_entry = {}
         self.identifier_to_entry = {}
         self.num_to_entry = {}
@@ -637,8 +635,6 @@ class Scope:
 
     def generate_library_function_declarations(self, code):
         # Generate extern decls for C library funcs used.
-        #if self.pow_function_used:
-        #      code.putln("%s double pow(double, double);" % Naming.extern_c_macro)
         pass
         
     def defines_any(self, names):
diff --git a/tests/errors/e_modop.pyx b/tests/errors/e_modop.pyx
deleted file mode 100644 (file)
index 926c3fb..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-def f():
-       cdef float flt1, flt2, flt3
-       flt1 = flt2 % flt3 # error
-_ERRORS = u"""
-/Local/Projects/D/Pyrex/Source/Tests/Errors2/e_modop.pyx:3:13: Invalid operand types for '%' (float; float)
-"""
diff --git a/tests/run/fmod.pyx b/tests/run/fmod.pyx
new file mode 100644 (file)
index 0000000..0bbd76b
--- /dev/null
@@ -0,0 +1,7 @@
+__doc__ = """
+    >>> fmod(7, 1.25)
+    0.75
+"""
+
+def fmod(double a, double b):
+    return a % b