CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   floating point error (

sivakuamr August 29, 2001 00:42

floating point error
hi all,

can anyone please tell me whats the source and the way to rectfy this error-this is killing me. thanks for the help.

John C. Chien August 29, 2001 01:14

Re: floating point error
(1). Mostly likely you have divide by zero or multiply by a very large number problem. When this happens, the resulting variable will exceed the range. (too big)(2). Before you do a divide operation, first check the denominator (the downstair variable) to make sure that it is not zero. (3).Use double precision to increase the range of the float point variable.

Greg Perkins August 29, 2001 03:12

Re: floating point error
As John says its probably divide by zero. On some machines checking for zero doesn't always catch all such errors, I usually check that its a small number. For computing x = y/d the C code might look like:

if (fabs(d) < MIN_REAL_NUMBER) then /* --- do something special */ else x = y/d;

where MIN_REAL_NUMBER is a small number close to zero. Something like 1e-10 usually works well.


John C. Chien August 29, 2001 03:45

Re: floating point error
(1). Yes, normally you don't check the variable directly to see whether it is equal to zero or not. (because of the round off error) (2)." if(a==0.0) {...;} " is not the best way to avoid divide by zero.

I. Dotsikas August 29, 2001 04:32

Re: floating point error
A quick and dirty trick is to add to the denominator a very small number. This could be something like 1.e-12 or a number near the accuracy limit of your computer. (check it before). This small number does not affect your results (devision with a big number like 1.e+3) but is bigger than zero so that not any NaN results occur. Addition (or substraction) of very big and very small numbers is always dangerous (what I used here). Multiplication is not sensitiv and provides you with correct results.

Anyway the best way is to check your code and try to find the mistake.

I. Dotsikas

sivakuamr August 29, 2001 12:21

Re: floating point error(to Mr. jhon c. chien)
hi jhon thanks, but this happens when i try to initialize the solution in Fluent5.4. moreover i do not perform any division or multiplication(other rhan the ones involved in general solution procedure). so how do i track down my source of error. thanks for the help. sivakumar

John C. Chien August 29, 2001 12:36

Re: floating point error(to Mr. jhon c. chien)
(1). Follow a tutorial sample first, to see if you are having the same problem. (2). If you still have the problem, contact the vendor's engineer. When using a commercial cfd code, you must follow only the working sample cases. If the sample cases don't work, stop using the code.

Matthias August 30, 2001 06:27

Re: floating point error
Hi Greg

This seam pretty nice. But how does it work if you implement this into a parallel code. What about pipeline stalling, pipeline-fillup ... The same question for cache oriented code implementation. I think "IF-statements" are far expensive tnen FLOP-operation? If ich check each denominator for this relation - the calculation time increases drastically. ( I know: Better as slow code than a garbage-code ... )

Are there better posibilities.

Dave August 30, 2001 08:36

Re: floating point error(to Mr. jhon c. chien)
Check your k and e values. It could be that they are zero when you try to initialize.

John C. Chien August 30, 2001 12:33

Re: floating point error
(1). So, commercial codes are slow and buggy. (2). Slow because they are doing some checking, buggy because they did not cover every possibility.

John C. Chien August 30, 2001 14:23

Re: floating point error(to Mr. jhon c. chien)
(1). What you are saying is when the initial value for k (turbulence kinetic energy) is zero (no turbulence and epsilon will be zero also), the computation will fail. (2). Then the code should set a non-zero initial value for k, and also check the k value constantly during the calculation (or iteration). I think, if the code is having problem with zero initial k value, then it also could run into troubles during the iterations.

Dave August 31, 2001 01:25

Re: floating point error(to Mr. jhon c. chien)
The default values of k and e are zero and you have to obtain appropriate values from your inlet conditions etc. When you are new to the code you just 'click' the button 'initialize' not knowing what values to use. Sometimes this results into a 'floating point error'.

John C. Chien August 31, 2001 01:37

Re: floating point error(to Mr. jhon c. chien)
(1). In that case, it must be the left-over garbage in that particular memory location which is causing the problem, when the inlet condition for k and epsilon are not explicitly specified by the user. (2). It shouldn't be that difficult to include the checking of the user's operation at this point, to force the user to properly specify the conditions.

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