Fortran Floating Point Error
Contents |
When computations go awry in your program, you may notice incorrect numbers in some output fields, even though your program continues to floating overflow fortran execute. Sometimes you may notice strings like INF and NaN in floating point coprocessor fault fortran fields where only numbers should be; these indicate certain kinds of floating-point exceptions (FPEs). INF means "infinity" and fortran floating point exception NaN means "not a number." Sometimes it's hard to find where these FPEs occur in your code, but you must find and fix them. They are useful only
Program Received Signal Sigfpe Floating Point Exception Erroneous Arithmetic Operation
as diagnostics, and they harm performance since each FPE interrupts the processor on which it occurs. So how do you find where FPEs are occuring in your code? Salting your code with print statements is hit-or-miss and invasive, and we do not recommend it. If you believe you have only a few FPEs, you are well advised to use a debugger like TotalView or dbx, which will often automatically point at the first FPE in your core file. But if you have many FPEs, weeding them out in this manner can be tedious. Error trapping An alternative and reliable method is called "trapping." By trapping, we mean setting a trap at your program's runtime that gets tripped when an FPE occurs, after which the program execution follows a prescribed course of your choice. This course is referred to as "handling" the error, where the handling you choose may cause the program to abort, print a diagnostic message, or provide a traceback. With certain methods of trapping, you can even provide a subroutine or function that changes the behavior of the floating-point arithmetic, though you should consult a numerical analyst about the consequences before handling errors in this manner. Since trapping and handling require extra processor time, you may wish to remove trapping/handling subroutine calls and compiler options after you have removed your program's FPEs. Both trapping
information about specific bugs or numerical errors found and fixed in GEOS-Chem, please see our Numerical issues discovered in GEOS-Chem page. Contents 1 Floating-point is an approximation to the real number system 2 Safe floating-point division 3 Testing for equality 3.1 Exact-value testing 3.1.1 Integers 3.1.2 Real numbers 3.1.3 Avoiding zero 3.2 Epsilon testing 3.2.1 Avoiding division by zero 3.2.2 Testing for values close to zero 3.2.3 Testing for values close to a non-zero number 4 Testing for non-representable values 4.1 NaN (Not-a-Number) 4.2 Infinity Floating-point is an approximation to the real number system The real number system may be thought of as a number line with the following characteristics: Zero is http://www.cisl.ucar.edu/news/01/tips/0209.fpe.html the primary reference point From zero, the line extends infinitely in both directions towards + Infinity and -Infinity Between any two real numbers, there are an infinite number of other real numbers A pictorial representation of the real number system looks like this: -Infinity <---------- 0 ---A---B----> +Infinity where between points A and B there are an infinite number of real numbered-values. It is important to realize that floating-point mathematics (as implemented in http://wiki.seas.harvard.edu/geos-chem/index.php/Floating_point_math_issues all modern computer systems) is never exact but is only an approximation to the real number system. In most programming languages, floating-point real numbers are composed of groups of 4 or 8 bytes. This means that floating-point numbers are not infinitely precise, but have a maximum precision. As a consequence, floating-point math operations (especially multiplication and division) can often lead to different results than one would normally anticipate. Here are the common number types used in both IDL and Fortran: IDL Number Type Fortran equivalent ESMF equivalent Number of bytes Approx. range byte BYTE - 1 0 to 255 fix INTEGER*2 ESMF_KIND_I2 2 0 - 32767 long INTEGER*4 ESMF_KIND_I4 4 -214700000 to -214700000 float REAL*4 ESMF_KIND_R4 4 -1e-38 to 1e+38 double REAL*8 ESMF_KIND_R8 8 -1e-312 to 1e+312 A pictorial representation of the floating point mathematics system would look like this: -Min Representable Value < . . . . . . 0 . . A . B . . > +Max Representable Value Because each floating-point number type is composed of a finite number of bytes, we can only represent numbers within a given range (see table above). Therefore, it is not possible to represent numbers to -Infinity or +Infinity with floating-point mathematics. Also, in on the floating-point number line pictured above, there are a finite number of possible
here for a quick overview of the site Help Center Detailed answers to any questions you might http://stackoverflow.com/questions/29433518/floating-point-error-gfortran have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million floating point programmers, just like you, helping each other. Join them; it only takes a minute: Sign up floating point error gfortran up vote 0 down vote favorite I am trying to debug a Fortran program. To catch floating point errors, I'm using the following compiler options for gfortran 4.9.0: FFLAGS1 = -std=f2003 -ffree-form -fdefault-real-8 -fdefault-double-8 \ fortran floating point -Ofast -fall-intrinsics -fcheck=all -m64 \ -fno-trapping-math -c \ -ffpe-trap=invalid,zero,overflow,underflow,precision,denormal -Wall With these options, the program fails at this line: read(ctrlUnit,*) slope_fasst, aspect when trying to read these inputs: 10.0 70.0 If I remove -ffpe-trap=invalid,zero,overflow,underflow,precision,denormal from the compiler options, it reads the following line just fine. Both variables are declared as real(8). In the input file, I've tried spaces, commas, etc. but see no changes. Does anyone have a suggestion? fortran gfortran floating-point-exceptions share|improve this question edited Apr 4 '15 at 12:45 francescalus 9,15341538 asked Apr 3 '15 at 13:52 user2417662 193 2 What error does the run-time system report ? And what is the program trying to read when it fails ? –High Performance Mark Apr 3 '15 at 13:56 I'm trying to read these inputs: 10.0 70.0 –user2417662 Apr 3 '15 at 17:20 Are those the only inputs that are being read? That's somewhat interesting, in that 10.0 and 70.0 are exactly representable in either single- or double