![]() |
Floating-Point Traps |
<<< Environment | Chapters |
Floating-point run-time errors are a sign that your program is processing invalid numerical values; these can come from programming errors or invalid input data. Some invalid values depend on the operation; for example, you are allowed to add zero to a number or multiply it by zero but division by zero is not permitted, hence zero is an invalid number for division.
Floating-point traps are a mechanism by which the hardware detects floating-point run-time errors. Unlike other run-time errors, you can decide whether the operating system reports these errors (via a signal) or ignores them: the right course of action depends on your application. Enabling a floating-point trap for a particular floating-point error ensures that the operating system raises the appropriate signal when that error occurs; disabling a floating-point trap means that no signal is raised, allowing processing to continue.
Note: Floating-point traps are sometimes called “interrupts” or “exceptions”; they must not be confused with C++ exceptions, however. |
IEEE standard 754-1985, which is supported by all computers that can run Parasolid, defines the range and precision for floating-point numbers, various special values and conditions, and six floating-point traps. Of these six, three are recommended for use in Parasolid-based programs. The other three are not useful for modeling work; their applications are generally confined to pure mathematics.
For more details of the exact meaning of IEEE floating-point traps, consult the processor manuals for your computer, which are usually available for download.
The IEEE definitions of the floating-point traps allow for any combination of them to be enabled or disabled. Some processors have a more limited set of options, concentrating on the useful combinations, and some processors do not provide floating-point traps at all. Enabling or disabling floating-point traps is usually done by means of calls to the C run-time library: there are some examples in Chapter 6, “Environment”.
When a floating-point trap is enabled and an instruction executed that creates the appropriate error condition, the trap is activated and generates the signal SIGFPE. When a floating-point trap is disabled and an instruction executed that creates the appropriate error condition, the trap is not activated and no signal is generated. Instead, the instruction produces a special result, listed in the tables in Section A.6, “Types of floating-point trap” for each kind of trap and explained in Section A.8, “Special floating-point values”. Execution of the program then continues with the next instruction.
These are defined by the IEEE 754-1985 standard. While it may seem that floating-point numbers should “just work” and not have all these complexities, ideal mathematical numbers are not easy for computers to represent, and the best current approaches to the problem are hundreds to thousands of times slower than IEEE floating-point.
This is a special value that essentially tells the processor: “This is not a number and cannot be used in mathematical calculations.” If the floating-point traps that can produce NaN are disabled, then once a single NaN is produced it will spread to any other numbers that are calculated using it. For example, any arithmetic operation involving a NaN will produce a result of NaN, irrespective of the other operands used.
IEEE floating-point numbers have a limited range of possible values. An operation that produces a value outside this range has “overflowed” the range. If the corresponding floating-point trap is disabled, a special value that says “this is infinity” is produced.
As well as a limited range of values, IEEE floating-point numbers have limited precision. Denormal values are those too small to be represented with full accuracy, but which can still be distinguished from zero. Parasolid expects that these will be generated from time to time, and works happily with them. Enabling the floating-point traps that detect and raise signals for denormal values will prevent Parasolid from working properly.
A third limitation of IEEE floating-point numbers is that they cannot represent some values exactly. For example, ordinary decimal numbers cannot represent the fraction 1/3 exactly: its value is 0.333333... recurring. This is normally dealt with by allowing the processor to round off the problematic value to the closest value that it can represent exactly. This is done automatically if the floating-point trap for an inexact value is disabled, and works very well. Enabling the floating-point traps that detect rounded values will prevent Parasolid from working properly.
On most current operating systems, the default is that all floating-point traps are disabled. If floating-point traps are enabled, and one goes off, the operating system will generate the signal SIGPFE; control is then passed to a registered signal handler for SIGFPE if one exists, or the application is shut down if there is no handler for SIGFPE.
For guidance with platforms where the default configuration for floating-point traps is not the recommended one for use with Parasolid, see Section 6.4, “Floating-point information”.
Some possible strategies for managing floating-point traps are given below.
This works well for simple programs where it is acceptable for Not A Number or Infinity to propagate through the data the program produces. For larger programs, it may not be satisfactory, but it is the only option if the platform cannot generate floating-point traps, or if the compiler assumes that they will never be turned on, and takes advantage of this in its optimizer.
Chapter 6, “Environment” shows you how to enable floating-point traps on each platform. Programs do not handle signals by default; to do so, they must register signal handlers.
This approach makes sure that floating-point traps are detected, since the operating system will terminate any program that generates (and does not handle) a floating-point trap. However, it is not suitable for programs that are used interactively.
For most applications, this is the best strategy. For information about how to register a signal handler and how Parasolid interacts with one, see Chapter 121, “Signal Handling” of the Functional Description manual. Where a platform requires special treatment for generating or handling SIGFPE, this is described in Chapter 6, “Environment”, of this manual.
Some processors have an optional way of treating denormal numbers: rather than generating them or loading them from memory, they will round them off to zero. This allows processes to run more quickly: managing all the details of denormal numbers makes programs run slower on many platforms.
Parasolid runs happily with denormals treated as zeroes, and if this mode of processing is acceptable to your application, you should consider using it. Where a platform requires special instructions for treating denormals as zeroes, this is specified in Chapter 6, “Environment”.
<<< Environment | Chapters |