# Problem when using pow on an integer

**URL:** <https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408>\
**Category:** General Discussion\
**Created:** [January 5, 2022, 10:29am UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408 "2022-01-05T10:29:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![romaingrvl](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@romaingrvl](https://community.freefem.org/u/romaingrvl)\
**Post date:** [January 5, 2022, 10:29am UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/1 "2022-01-05T10:29:26Z")

</div>

Hello,

I found a little glitch with the native function pow when it is used on an integer. I derive my equations to be solved with Mathematica because they are rather lenghty and it converts the formal expressions into code, in particular by using the pow function for powers. Then I copy/paste the equations in my code, using the varf formulation. It is not the first time I did this procedure but yesterday my code was not able to compile and the error was

Assertion fail : ((iscmplx) == IsComplexType::value)

on the line where I “evaluate” the varf expression. I finally figured out what was the problem and I think it is something that have to be fixed. Indeed, it comes from the power of an integer M, pow(M,2), if I replace it by M^2 it works but with pow it seems like FreeFem treat it like a complex number for some mysterious reason. And of course, if I change the definition of M by declaring it as a real, real M=2, the pow function works. Therefore it is really a little issue of the function pow when it is acting on a real.

Does anyone have the same problem ? I use the last version of FF++ on Ubuntu 20.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 5, 2022, 10:47am UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/2 "2022-01-05T10:47:02Z")

</div>

Please provide a minimal working example that reproduces this behavior.

---

<div class="post-metadata">

**Author:** ![romaingrvl](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@romaingrvl](https://community.freefem.org/u/romaingrvl)\
**Post date:** [January 5, 2022, 12:07pm UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/3 "2022-01-05T12:07:16Z")

</div>

Of course, on my computer the following minimal code do not compile :

int M = 2;  
real n = pow(M,2);

But this work :

int M = 2;  
real n = M^2;

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 5, 2022, 1:16pm UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/4 "2022-01-05T13:16:31Z")

</div>

This is expected. Here are the various ways to use `pow` (which is not designed for integers but rather floating-point scalars, as in C++ [pow - C++ Reference](https://www.cplusplus.com/reference/cmath/pow/)):

```auto
int M = 2;
real n = pow(M,2.0);

M = 2;
n = pow(real(M),2);

M = 2;
n = M^2;

```

---

<div class="post-metadata">

**Author:** ![romaingrvl](https://avatars.discourse-cdn.com/v4/letter/r/58956e/32.png) [@romaingrvl](https://community.freefem.org/u/romaingrvl)\
**Post date:** [January 5, 2022, 1:38pm UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/5 "2022-01-05T13:38:20Z")

</div>

Thank you, I was used to C++ before using FF++ but I hadn’t encountered this subtlety before.

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [January 6, 2022, 9:03am UTC](https://community.freefem.org/t/problem-when-using-pow-on-an-integer/1408/6 "2022-01-06T09:03:13Z")

</div>

The problem is due to automatique cast  
a integer can be casted in real or in complex value,

And I make a mistake by default it is in complex

```auto
cout << pow(2,2) << endl;

```

print : (4,0)

I will correct in the minute in develop (thanks the the help) F. Hecht.  
see [remove load MUMPS\_seq in examples/tutorial/tgv-test.edp · FreeFem/FreeFem-sources@bd079ab · GitHub](https://github.com/FreeFem/FreeFem-sources/commit/bd079ab71d4bcbe2e6ddda1179889e57e3460889)
