CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Bugs (https://www.cfd-online.com/Forums/openfoam-bugs/)
-   -   Foam::pow() function not working with integer fraction (https://www.cfd-online.com/Forums/openfoam-bugs/68216-foam-pow-function-not-working-integer-fraction.html)

dominik_christ September 10, 2009 10:18

Foam::pow() function not working with integer fraction
 
Hello everyone,
the following bug is present in both OF1.5 and OF1.6:

You would expect the following functions to return the same value

Info << pow(turbulence->epsilon(), (1./2.) );
Info << pow(turbulence->epsilon(), (1/2) );

Both compile without error or warning. However, the second line only returns the value 1 in all fields. The first line returns the square root of the epsilon field just as expected.

NB: This bug does not occur with plain integers, so
Info << pow(turbulence->epsilon(), 2. );
Info << pow(turbulence->epsilon(), 2 );
will give you the same (correct) result.

Regards,
Dominik Christ

P.S.: As this is such a fundamental function, I wonder why it passed unnoticed for so long ...

mattijs September 10, 2009 12:11

The compiler will evaluate 1/2 as integer division. Hence it will be doing a pow(.., 0) which returns 1 as it should. Apart from this you could also use sqrt or sqr.

dominik_christ September 10, 2009 13:32

Oh, of course, that's the reason.
Thanks Mattijs for pointing that out to me!

wadekar August 6, 2015 12:19

Hello

I am also struggling with same problem, but i want to do the power (1/3).

Code:

scalar rad = Foam::pow(Vol,1.0/3.0);
Vol is a scalar.

My main problem is that- when i using
Code:

scalar rad = Foam::pow(Vol,1.0/3.0);
then it gives right result without any problem, but its not running in parallel.

And, when i used
Code:

scalar rad = Foam::pow(Vol,1/3);
then i am getting 1, but code is running in parallel.


All times are GMT -4. The time now is 06:35.