Repository: pyccel/pyccel

Issue:
Builtin abs function not working in c language
**Describe the bug**
Builtin abs function not implemented in c language

**To Reproduce**
Provide code to reproduce the behavior:
```python
@types('int')
def f(x):
    return abs(x)
```

**Error : **
ERROR at code generation stage
pyccel:
 |fatal [codegen]: first.py [3,11]| Pyccel has encountered syntax that has not been implemented yet. (<pyccel.ast.builtins.PythonAbs object at 0x7fa62ca792b0>)


**Language**
c


Discussion:
EmilyBourne: >if it show true that mean that Git picks up (chmod : permission) changes

Ah! I had not realised that these files were present in the repository already and that this is what the change was. I think it makes sense for `run_tests_py3.sh` to be executable so that you can do:
`./tests/run_tests_py3.sh`
but `doc/latex_macros.sty` shouldn't be executable anyway. So if you want to keep the change to this file then I am happy with that, but it should be mentioned in the PR description

Patch:
diff --git a/pyccel/codegen/printing/ccode.py b/pyccel/codegen/printing/ccode.py
index 45f6242a..9a5f4601 100644
--- a/pyccel/codegen/printing/ccode.py
+++ b/pyccel/codegen/printing/ccode.py
@@ -396,6 +396,17 @@ class CCodePrinter(CodePrinter):
 
     # ============ Elements ============ #
 
+    def _print_PythonAbs(self, expr):
+        if expr.arg.dtype is NativeReal():
+            self._additional_imports.add("math")
+            func = "fabs"
+        elif expr.arg.dtype is NativeComplex():
+            self._additional_imports.add("complex")
+            func = "cabs"
+        else:
+            func = "abs"
+        return "{}({})".format(func, self._print(expr.arg))
+
     def _print_PythonFloat(self, expr):
         value = self._print(expr.arg)
         type_name = self.find_in_dtype_registry('real', expr.precision)


Test patch:
diff --git a/tests/epyccel/test_builtins.py b/tests/epyccel/test_builtins.py
new file mode 100644
index 00000000..2b597fb1
--- /dev/null
+++ b/tests/epyccel/test_builtins.py
@@ -0,0 +1,41 @@
+# pylint: disable=missing-function-docstring, missing-module-docstring/
+
+from pyccel.epyccel import epyccel
+from pyccel.decorators import types
+
+def test_abs_i(language):
+    @types('int')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(0) == f2(0)
+    assert f1(-5) == f2(-5)
+    assert f1(11) == f2(11)
+
+def test_abs_r(language):
+    @types('real')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(0.00000) == f2(0.00000)
+    assert f1(-3.1415) == f2(-3.1415)
+    assert f1(2.71828) == f2(2.71828)
+
+
+
+def test_abs_c(language):
+    @types('complex')
+    def f1(x):
+        return abs(x)
+
+    f2 = epyccel(f1, language=language)
+
+    assert f1(3j + 4) == f2(3j + 4)
+    assert f1(3j - 4) == f2(3j - 4)
+    assert f1(5j + 0) == f2(5j + 0)
+    assert f1(0j + 5) == f2(0j + 5)
+    assert f1(0j + 0) == f2(0j + 0)