Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library

C Standard Library

C Standard Library

is a header file in the C standard library used to control the floating-point environment (Floating-Point Environment).

Introduced in the C99 standard, it provides control and query capabilities for floating-point exceptions, rounding modes, and other floating-point states.


The main purposes are:


1. Floating-point exceptions

Floating-point exceptions refer to special situations that occur during floating-point operations, for example:

These exceptions are represented by floating-point status flags and can be detected and handled through the functions in .


2. Rounding modes

The rounding mode controls how the results of floating-point operations are rounded. The following rounding modes are defined:


3. Main functions and macros

Provides a set of functions and macros for manipulating the floating-point environment and handling floating-point exceptions.

Floating-point exception handling

Function/macro Description
feclearexcept(int excepts) Clear the specified floating-point exception flags
feraiseexcept(int excepts) Raise the specified floating-point exception
fetestexcept(int excepts) Test whether the specified floating-point exception has occurred
fegetexceptflag(fexcept_t *flagp, int excepts) Get the floating-point exception flag status
fesetexceptflag(const fexcept_t *flagp, int excepts) Set the floating-point exception flag status

Rounding mode control

Function/macro Description
fegetround(void) Get the current rounding mode
fesetround(int round) Set the current rounding mode

Floating-point environment control

Function/macro Description
fegetenv(fenv_t *envp) Save the current floating-point environment
fesetenv(const fenv_t *envp) Restore the floating-point environment
feholdexcept(fenv_t *envp) Save the current floating-point environment and clear the exception flags
feupdateenv(const fenv_t *envp) Restore the floating-point environment and raise exceptions

4. Example

The following is an example of using example, showing how to detect floating-point exceptions and control rounding modes:

Example

#include

#include

#include

int

main

(

)

{

   

// Enable floating-point exception detection

    feclearexcept

(

FE_ALL_EXCEPT

)

;

   

// Raise a division-by-zero exception

   

double

x

=

1.0

,

y

=

0.0

;

   

double

z

=

x

/

y

;

   

// Check whether a division-by-zero exception has occurred

   

if

(

fetestexcept

(

FE_DIVBYZERO

)

)

{

       

printf

(

"Divide by zero exception occurred.\n"

)

;

   

}

   

// Set the rounding mode to round toward zero

    fesetround

(

FE_TOWARDZERO

)

;

   

// Test the rounding mode

   

double

a

=

1.5

;

   

double

b

=

rint

(

a

)

;

// Round to an integer

   

printf

(

"Rounding 1.5 toward zero: %.1f\n"

,

b

)

;

   

return

0

;

}

The output is:

Divide by zero exception occurred.

Rounding 1.5 toward zero: 1.0

Detecting floating-point exceptions

Example

#include

#include

#include

#pragma STDC FENV_ACCESS ON

void

check_exceptions

(

)

{

   

// Clear all exception flags

    feclearexcept

(

FE_ALL_EXCEPT

)

;

   

   

// Perform operations that may raise exceptions

   

double

x

=

0.0

;

   

double

y

=

1.0

/

x

;

 

// Division by zero

   

   

// Check a specific exception

   

if

(

fetestexcept

(

FE_DIVBYZERO

)

)

{

       

printf

(

"Division by zero occurred!\n"

)

;

   

}

   

   

// Check for any exception

   

if

(

fetestexcept

(

FE_ALL_EXCEPT

)

)

{

       

printf

(

"At least one floating-point exception occurred\n"

)

;

   

}

}

int

main

(

)

{

    check_exceptions

(

)

;

   

return

0

;

}

Controlling the rounding direction

Example

#include

#include

#pragma STDC FENV_ACCESS ON

void

test_rounding

(

double

num

)

{

   

int

modes

[

]

=

{

FE_TONEAREST

,

FE_UPWARD

,

FE_DOWNWARD

,

FE_TOWARDZERO

}

;

   

const

char

names

[

]

=

{

"Nearest"

,

"Up"

,

"Down"

,

"Toward zero"

}

;

   

   

for

(

int

i

=

0

;

i

<

4

;

i

++

)

{

       

if

(

fesetround

(

modes

[

i

]

)

==

0

)

{

           

printf

(

"Rounding %s: %.1f -> %.0f\n"

,

                  names

[

i

]

,

num

,

rint

(

num

)

)

;

       

}

   

}

}

int

main

(

)

{

    test_rounding

(

2.5

)

;

 

// Test the result of 2.5 under different rounding modes

    test_rounding

(

2.5

)

;

// Test the result of -2.5 under different rounding modes

   

return

0

;

}

Saving and restoring the floating-point environment

Example

#include

#include

#include

#pragma STDC FENV_ACCESS ON

void

sensitive_calculation

(

)

{

    fenv_t env

;

   

   

// Save the current environment

    fegetenv

(

&

env

)

;

   

   

// Set a strict environment: trap all exceptions

    feclearexcept

(

FE_ALL_EXCEPT

)

;

   

   

// Perform critical computations

   

double

result

=

sqrt

(

1.0

)

;

 

// Invalid operation

   

   

if

(

fetestexcept

(

FE_INVALID

)

)

{

       

printf

(

"Invalid operation detected in sensitive calculation\n"

)

;

   

}

   

   

// Restore the original environment

    fesetenv

(

&

env

)

;

}

int

main

(

)

{

    sensitive_calculation

(

)

;

   

return

0

;

}

Temporarily masking exceptions

Example

#include

#include

#include

#pragma STDC FENV_ACCESS ON

void

temp_disable_exceptions

(

)

{

    fenv_t env

;

   

   

// Save the environment and temporarily mask all exceptions

    feholdexcept

(

&

env

)

;

   

   

// Perform operations that may raise exceptions

   

double

x

=

0.0

;

   

double

y

=

1.0

/

x

;

 

// No exception is raised

   

   

printf

(

"Division by zero was performed but not reported\n"

)

;

   

   

// Restore the environment and handle exceptions that occurred earlier

    feupdateenv

(

&

env

)

;

   

   

// Now the exceptions can be checked

   

if

(

fetestexcept

(

FE_DIVBYZERO

)

)

{

       

printf

(

"Division by zero was detected after restoring environment\n"

)

;

   

}

}

int

main

(

)

{

    temp_disable_exceptions

(

)

;

   

return

0

;

}

Precise computation example

Example

#include

#include

#include

#pragma STDC FENV_ACCESS ON

double

precise_sum

(

double

a

,

double

b

)

{

   

int

original_round

=

fegetround

(

)

;

    fenv_t env

;

   

   

// Save the environment and set the highest precision mode

    fegetenv

(

&

env

)

;

    fesetround

(

FE_TONEAREST

)

;

    feclearexcept

(

FE_ALL_EXCEPT

)

;

   

   

double

result

=

a

b

;

   

   

// Check whether the computation is exact

   

if

(

fetestexcept

(

FE_INEXACT

)

)

{

       

printf

(

"Warning: Sum was not exact, possible precision loss\n"

)

;

   

}

   

   

// Restore the original environment

    fesetround

(

original_round

)

;

    fesetenv

(

&

env

)

;

   

   

return

result

;

}

int

main

(

)

{

   

double

a

=

1.0

/

3.0

;

   

double

b

=

2.0

/

3.0

;

   

   

printf

(

"Sum: %.17g\n"

,

precise_sum

(

a

,

b

)

)

;

   

return

0

;

}

Exception handling wrapper

Example

#include

#include

#include

#pragma STDC FENV_ACCESS ON

typedef

double

(

math_func

)

(

double

)

;

double

safe_math

(

math_func f

,

double

x

,

int

error

)

{

    fenv_t env

;

   

error

=

0

;

   

    feclearexcept

(

FE_ALL_EXCEPT

)

;

    fegetenv

(

&

env

)

;

   

   

double

result

=

f

(

x

)

;

   

   

int

exceptions

=

fetestexcept

(

FE_ALL_EXCEPT

)

;

   

if

(

exceptions

)

{

       

error

=

exceptions

;

       

if

(

exceptions

&

FE_INVALID

)

printf

(

"Invalid operation\n"

)

;

       

if

(

exceptions

&

FE_DIVBYZERO

)

printf

(

"Division by zero\n"

)

;

       

if

(

exceptions

&

FE_OVERFLOW

)

printf

(

"Overflow\n"

)

;

       

if

(

exceptions

&

FE_UNDERFLOW

)

printf

(

"Underflow\n"

)

;

       

if

(

exceptions

&

FE_INEXACT

)

printf

(

"Inexact result\n"

)

;

   

}

   

    fesetenv

(

&

env

)

;

   

return

result

;

}

int

main

(

)

{

   

int

error

;

   

   

double

a

=

safe_math

(

sqrt

,

1.0

,

&

error

)

;

   

printf

(

"sqrt(-1) = %f (error: %d)\n"

,

a

,

error

)

;

   

   

double

b

=

safe_math

(

log

,

0.0

,

&

error

)

;

   

printf

(

"log(0) = %f (error: %d)\n"

,

b

,

error

)

;

   

   

return

0

;

}

5. Notes

provides control over the floating-point environment, including detection and handling of floating-point exceptions, setting rounding modes, and querying and modifying floating-point status. It is a powerful tool for high-precision floating-point computation and debugging floating-point problems, especially suitable for scientific computing and engineering applications. By using , developers can better control the behavior of floating-point operations, ensuring the accuracy and reliability of computation results.