# Numerical precision

 Register Blogs Members List Search Today's Posts Mark Forums Read

 March 12, 2010, 19:54 Numerical precision #1 Senior Member   Join Date: Nov 2009 Posts: 411 Rep Power: 12 Sponsored Links Hello, I've recently implemented 3 small CFD solvers one in C++, one in Fortran 95/2003 and one in Matlab for comparation purposes, both compiled codes were parallelized using OpenMP. I've noticed that for the first iterations all 3 solvers have comparable precision (about 10 digits), after a while (a few hundreds of iterations) the C++ performance was degraded. I wonder if some of you have the same experience with numerical precision in C++. As a note: - Matlab works by default in double precision - in Fortran I've used double precision (Intel Fortran ) - in C++ double (Intel C++) Do

 March 12, 2010, 22:28 #2 Senior Member   Join Date: Nov 2009 Posts: 411 Rep Power: 12 A simple example on a 64 bits computer acos(-1) , exact value = pi C/C++ using long double: 3.1415926535897932385128089594061862 Fortran 95 using selected_real_kind(32,99) 3.1415926535897932384626433832795028 arbitrary precision calculator, precision set to 36 digits: 3.14159265358979323846264338327950288 C/C++ gets about 18 exact decimals vs 15 for a simple double !!!!!

 February 11, 2011, 07:36 #3 Member   Join Date: Mar 2009 Location: Istanbul, Turkiye Posts: 45 Rep Power: 10 Hi Do I have a similar problem in C++ and MATLAB like yours. I have two cfd code that do same calculation. the two codes give exactly the same results for low reynolds numbers. For high reynolds number, i.e. 10000, MATLAB code converges succesfully, but c++ code does not converge. Do you know any way to increase the numerical precision of C++? Thanks, Gemini

 February 11, 2011, 10:53 #4 Senior Member   Join Date: Nov 2009 Posts: 411 Rep Power: 12 If you are sure both codes have no bugs, than you can try to play with precision in C++. Try first to convert floats to doubles, and after that doubles to long double. What I suspect is that your codes have different turbulence models, this will explain why for low Re numbers you have the same solution and for higher Re your results differs or one of the code does not converge. Do

February 11, 2011, 20:54
#5
Senior Member

Arjun
Join Date: Mar 2009
Location: Nurenberg, Germany
Posts: 700
Rep Power: 19
Quote:
 Originally Posted by DoHander A simple example on a 64 bits computer acos(-1) , exact value = pi C/C++ using long double: 3.1415926535897932385128089594061862 Fortran 95 using selected_real_kind(32,99) 3.1415926535897932384626433832795028 arbitrary precision calculator, precision set to 36 digits: 3.14159265358979323846264338327950288 C/C++ gets about 18 exact decimals vs 15 for a simple double !!!!!

this may be down to the algorithm used in calculating PI. It may be different in fortran and c/c++. This is why it is difficult to conclude anything about precision from this info.

Other than that i have written cfd solvers in c++ only and i have never faced problems at least in double precision.

There is one thing though, different compilers initialise vairables differently. i have noticed that if one is not careful about it then it might cause problems. (I am assuming this is not the case but i think it is worth mentioning because sometimes most obvious bugs are overlooked).

 February 12, 2011, 16:54 #6 Senior Member   Martin Hegedus Join Date: Feb 2011 Posts: 479 Rep Power: 12 Sorry to say, but if the difference has become noticeable, and you are running in double precision, then there is probably something substantially different between the codes. For example you may be indexing incorrectly. Of there may be a bug such as overrunning an array or something hasn't been initialized correctly. Of course I'm not saying that the difference of precision isn't causing your problem. But, if it was, then you would also see noticeable differences if you were running optimized or unoptimized, the flow of the codes were different, some functions were inlined or not, etc.

 Thread Tools Display Modes Linear Mode

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are On Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kk81 OpenFOAM Bugs 0 March 11, 2010 07:14 matheusguzella ANSYS Meshing & Geometry 2 October 7, 2009 10:15 morxio Main CFD Forum 3 April 27, 2007 10:38 ado Main CFD Forum 3 October 12, 2000 08:20 Eleuterio TORO Main CFD Forum 0 December 18, 1998 13:41