CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   The precision of "Foam::pow"? (https://www.cfd-online.com/Forums/openfoam-programming-development/159971-precision-foam-pow.html)

sharonyue September 28, 2015 11:25

The precision of "Foam::pow"?
 
Hello guys,

I use OpenFOAM to do a simple calculation:

Code:

            scalar z = Foam::exp(Foam::sqr(0.544830258187397)/2.0);
            Info<<"z = "<<z<<endl;
            Info<<"z 16= "<<Foam::pow(z, 16.0)<<endl;

This is the output:

Code:

z = 1.16
z 16= 10.748

However, especially this z, it should be 1.16219666037989. Then my z16 will be 11.0783.

This is what I got from matlab:
Code:

z = 1.1621966603798944501
z6 = 11.0783

How can I handle this? if I use z with 1.16, z16 is not precise.

Thanks guys.

Henning86 September 28, 2015 11:31

does it change if you change the writePrecision in the controlDict?

sharonyue September 28, 2015 11:38

update, sorry, sorry, I have a typo in my code....Its my fault not the code

------------------------------------------------------------------


Quote:

Originally Posted by Henning86 (Post 565751)
does it change if you change the writePrecision in the controlDict?

Hi,

yeah, I tried in my complete solver to debug, it does not have any effect.

And this code above (just a test) does not need controlDict file..

Code:

#include "fvCFD.H"

int main()
{
            double sigma = 0.544830258187397;
        Info<<"sigma = "<<sigma<<endl;
        Info<<"Foam::sqr(sigma)/2.0 ="<<Foam::sqr(0.544830258187397)/2.0<<endl;
        Info<<"Foam::exp(Foam::sqr(sigma)/2.0) = "<<Foam::exp(Foam::sqr(sigma)/2.0)<<endl;
        double z = Foam::exp(Foam::sqr(sigma)/2.0);
        Info<<"Foam::pow(ans, 16) = "<<Foam::pow(z, 16.0)<<endl;
}

Code:

sigma = 0.54483
Foam::sqr(sigma)/2.0 =0.14842
Foam::exp(Foam::sqr(sigma)/2.0) = 1.16 //????????????????
Foam::pow(ans, 16) = 10.748


The key of this problem is: this function:

Foam::exp(Foam::sqr(0.544830258187397)/2.0)

is 1.16.

But it should be 1.16219666037989

Henning86 September 29, 2015 08:02

The problem is probably the Info command

try Info.precision(16) i dont know what the exact command is.


google "precision cout". the message stream class should be similar


All times are GMT -4. The time now is 01:31.