Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C99
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Example === The following annotated example C99 code for computing a continued fraction function demonstrates the main features: <syntaxhighlight lang=C line highlight="9,11,13,15,21,23,25,36,42"> #include <stdio.h> #include <math.h> #include <float.h> #include <fenv.h> #include <tgmath.h> #include <stdbool.h> #include <assert.h> double compute_fn(double z) // [1] { #pragma STDC FENV_ACCESS ON // [2] assert(FLT_EVAL_METHOD == 2); // [3] if (isnan(z)) // [4] puts("z is not a number"); if (isinf(z)) puts("z is infinite"); long double r = 7.0 - 3.0/(z - 2.0 - 1.0/(z - 7.0 + 10.0/(z - 2.0 - 2.0/(z - 3.0)))); // [5, 6] feclearexcept(FE_DIVBYZERO); // [7] bool raised = fetestexcept(FE_OVERFLOW); // [8] if (raised) puts("Unanticipated overflow."); return r; } int main(void) { #ifndef __STDC_IEC_559__ puts("Warning: __STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported."); // [9] #endif #pragma STDC FENV_ACCESS ON #ifdef TEST_NUMERIC_STABILITY_UP fesetround(FE_UPWARD); // [10] #elif TEST_NUMERIC_STABILITY_DOWN fesetround(FE_DOWNWARD); #endif printf("%.7g\n", compute_fn(3.0)); printf("%.7g\n", compute_fn(NAN)); return 0; } </syntaxhighlight> Footnotes: # Compile with: {{code|lang=bash|1=gcc -std=c99 -mfpmath=387 -o test_c99_fp test_c99_fp.c -lm}} # As the IEEE 754 status flags are manipulated in this function, this #pragma is needed to avoid the compiler incorrectly rearranging such tests when optimising. (Pragmas are usually implementation-defined, but those prefixed with <code>STDC</code> are defined in the C standard.) # C99 defines a limited number of expression evaluation methods: the current compilation mode can be checked to ensure it meets the assumptions the code was written under. # The special values such as [[NaN]] and positive or negative infinity can be tested and set. # <code>long double</code> is defined as IEEE 754 double extended or quad precision if available. Using higher precision than required for intermediate computations can minimize [[round-off error]]<ref name=Baleful>{{cite web |url=https://www.cs.berkeley.edu/~wkahan/ieee754status/baleful.pdf |title=The Baleful Effect of Computer Benchmarks upon Applied Mathematics, Physics and Chemistry| author=William Kahan |date=11 June 1996}}</ref> (the [[typedef]] <code>double_t</code> can be used for code that is portable under all <code>FLT_EVAL_METHOD</code>s). # The main function to be evaluated. Although it appears that some arguments to this continued fraction, e.g., 3.0, would lead to a divide-by-zero error, in fact the function is well-defined at 3.0 and division by 0 will simply return a +infinity that will then correctly lead to a finite result: IEEE 754 is defined not to trap on such exceptions by default and is designed so that they can very often be ignored, as in this case. (If <code>FLT_EVAL_METHOD</code> is defined as 2 then all internal computations including constants will be performed in long double precision; if <code>FLT_EVAL_METHOD</code> is defined as 0 then additional care is need to ensure this, including possibly additional casts and explicit specification of constants as long double.) # As the raised divide-by-zero flag is not an error in this case, it can simply be dismissed to clear the flag for use by later code. # In some cases, other exceptions may be regarded as an error, such as overflow (although it can in fact be shown that this cannot occur in this case). # <code>__STDC_IEC_559__</code> is to be defined only if "Annex F IEC 60559 floating-point arithmetic" is fully implemented by the compiler and the C library (users should be aware that this macro is sometimes defined while it should not be). # The default rounding mode is round to nearest (with the even rounding rule in the halfway cases) for IEEE 754, but explicitly setting the rounding mode toward + and - infinity (by defining <code>TEST_NUMERIC_STABILITY_UP</code> etc. in this example, when debugging) can be used to diagnose numerical instability.<ref>{{cite web|url=https://www.cs.berkeley.edu/~wkahan/Mindless.pdf | title=How Futile are Mindless Assessments of Roundoff in Floating-Point Computation? | author=William Kahan |date=11 January 2006}}</ref> This method can be used even if <code>compute_fn()</code> is part of a separately compiled binary library. But depending on the function, numerical instabilities cannot always be detected.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)