This chapter opened with a discussion of issues that affect portability. One way to determine what constitutes a portable program is to take, in effect, the intersection of the capabilities across all CommonPoint platforms for each feature. Here is such a list of specific strategies:
TIP
In cases requiring extra range in long double, consider supporting two implementations, a straightforward one for platforms whose long double type offers extra range as well as precision, and a more carefully programmed one for platforms offering the exponent range of double and no more.
- Use explicit casting to ensure that narrow subexpressions are computed to a wider type.
- Use the proposed Floating-Point C Extensions features in
Numerics.h
and FPEnvironment.h
. Use the fenv_access pragma when manipulating the environment. Don't depend on compiler support of hexadecimal floating constants, but do expect sufficiently accurate binary-decimal conversion that any desired binary value can be fully specified by 9 digits for type float and 17 digits for type double.
TIP
If you develop on a platform supporting hexadecimal floating constants, isolate them in one or more header files in the event you should need to express them in decimal form.
- Use the underflow flag to defend against loss of precision in tiny numbers when the loss matters. Don't assume that all compilers will raise underflow in precisely the same circumstances. See "Alternatives" on page 219 for more details.
- Assume that the elementary functions are accurate to within a few units in the last place of the result. Assume that they're monotonic where appropriate, that they remain within their mathematical range (for example,
for all finite x), and that they satisfy mathematical identities such as
to within roundoff.
- Don't depend on the byte order of numbers in memory. Use stream operators for file I/O and use manipulation functions such as SignBit, Logb, and Scalb to decompose a floating-point value into its constituent sign, exponent, and significand.
- Use quiet NaNs, as supported by the system.
- Don't depend on the value of an integral result when the mathematical result cannot be held exactly in the integral type. The invalid flag is raised, but the result is platform-dependent.
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.
Generated with WebMaker