C/fenv.h

类介绍:

此头声明了一组函数和宏,用于访问浮点环境以及特定类型。浮点环境维护一系列状态标志和特定控制模式。 具体关于浮点环境的内容取决于实现,但状态标志通常包括浮点异常及其相关信息,并且控制模式至少包括舍入方向。

1.feclearexcept 函数

int feclearexcept (int excepts);

函数介绍:尝试清除excepts(异常)指定的浮点异常。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

macro value description
FE_DIVBYZERO Pole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACT Inexact: the result is not exact.
FE_INVALID Domain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOW Overflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOW Underflow range error: The result is too small in magnitude to be represented as a value of the return type.

返回值:如果异常中的所有异常都已成功清除(或者异常为零),则为零。否则,返回一个非零值。

/* feclearexcept, fetestexcept example */
#include <stdio.h>      /* printf */
#include <math.h>       /* sqrt */
#include <fenv.h>       /* feclearexcept, fetestexcept, FE_ALL_EXCEPT, FE_INVALID */
#pragma STDC FENV_ACCESS on

int main ()
{
  feclearexcept (FE_ALL_EXCEPT);
  sqrt(-1);
  if (fetestexcept(FE_INVALID)) printf ("sqrt(-1) raises FE_INVALID\n");
  return 0;
}

output:


sqrt(-1) raises FE_INVALID

2. feraiseexcept 函数

int feraiseexcept (int excepts);

函数介绍:尝试提高例外指定的浮点异常。如果指定了多个异常,则不指定引发它们的顺序。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

macro value description
FE_DIVBYZERO Pole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACT Inexact: the result is not exact.
FE_INVALID Domain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOW Overflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOW Underflow range error: The result is too small in magnitude to be represented as a value of the return type.
FE_ALL_EXCEPT All exceptions (selects all of the exceptions supported by the implementation).

返回值:返回 零 ,如果异常中的所有异常都成功抛出(或者异常为零)。否则,返回一个非零值。

/* feraiseexcept example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* feraiseexcept, fetestexcept, FE_ALL_EXCEPT, FE_INVALID */
#pragma STDC FENV_ACCESS on

double fn (double x) 
{  /* some function for which zero is a domain error */
  if (x==0.0) 
      feraiseexcept(FE_INVALID);
  return x;
}

int main ()
{
  feclearexcept (FE_ALL_EXCEPT);
  fn (0.0);
  if (fetestexcept(FE_INVALID)) 
         printf ("FE_INVALID raised\n");
  return 0;
}

output:

FE_INVALID raised

3. fegetexceptflag 函数

int fegetexceptflag (fexcept_t* flagp, int excepts);

函数介绍:试图将excepts指定的浮点异常的表示存储到由flagp指向的fexcept_t对象中。

参数:

fexcept_t* flagp:指向存储表示的fexcept_t对象的指针。

int excepts:

FE_DIVBYZERO Pole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACT Inexact: the result is not exact.
FE_INVALID Domain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOW Overflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOW Underflow range error: The result is too small in magnitude to be represented as a value of the return type.
FE_ALL_EXCEPT All exceptions (selects all of the exceptions supported by the implementation).

返回值:如果表示已成功存储,则为零。否则,返回非零值。

4. fesetexceptflag 函数

int fesetexceptflag (const fexcept_t* flagp, int excepts);

函数介绍:试图用存储在由flagp指向的对象中的状态来设置异常。

参数:

const fexcept_t* flagp:指向fexcept_t对象的指针,该对象具有浮点异常的表示。通过调用fegetexceptflag(至少由excepts指定的异常),可以预先设置flagp所指向的值。

int excepts:

FE_DIVBYZERO Pole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACT Inexact: the result is not exact.
FE_INVALID Domain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOW Overflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOW Underflow range error: The result is too small in magnitude to be represented as a value of the return type.
FE_ALL_EXCEPT All exceptions (selects all of the exceptions supported by the implementation).

返回值:返回 0,如果函数成功设置了(或者异常为0)中的标志。否则,返回非零值。

5. fegetround 函数

int fegetround (void);

函数介绍:返回一个值,该值指示舍入方向模式,如当前浮点环境中指定的那样。

返回值:如果函数成功地确定了当前的舍入模式并得到了实现的支持,则函数返回一个定义了相应宏的值:

FE_DOWNWARD Round downward.
FE_TONEAREST Round to nearest.
FE_TOWARDZERO Round toward zero.
FE_UPWARD Round upward.
/* fegetround / rint example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* fegetround, FE_* */
#include <math.h>       /* rint */

int main ()
{
  printf ("rounding using ");
  switch (fegetround()) {
    case FE_DOWNWARD: printf ("downward"); break;
    case FE_TONEAREST: printf ("to-nearest"); break;
    case FE_TOWARDZERO: printf ("toward-zero"); break;
    case FE_UPWARD: printf ("upward"); break;
    default: printf ("unknown");
  }
  printf (" rounding:\n");

  printf ( "rint (2.3) = %.1f\n", rint(2.3) );
  printf ( "rint (3.8) = %.1f\n", rint(3.8) );
  printf ( "rint (-2.3) = %.1f\n", rint(-2.3) );
  printf ( "rint (-3.8) = %.1f\n", rint(-3.8) );
  return 0;
}

output:

Rounding using to-nearest rounding:
rint (2.3) = 2.0
rint (3.8) = 4.0
rint (-2.3) = -2.0
rint (-3.8) = -4.0

6. fesetround 函数

int fesetround (int rdir);

函数介绍:将 int rdir 设置为浮点环境的当前舍入方向模式。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

int rdir:

FE_DOWNWARD Round downward.
FE_TONEAREST Round to nearest.
FE_TOWARDZERO Round toward zero.
FE_UPWARD Round upward.

返回值:如果已成功设置所请求的舍入方向,则为零。

/* fesetround example */
#include <stdio.h>      /* printf */
#include <fenv.h>       /* fesetround, FE_* */
#include <math.h>       /* rint */
#pragma STDC FENV_ACCESS on

int main ()
{
  printf ("rounding -3.8:\n");

  fesetround(FE_DOWNWARD);
  printf ("FE_DOWNWARD: %.1f\n", rint(-3.8));

  fesetround(FE_TONEAREST);
  printf ("FE_TONEAREST: %.1f\n", rint(-3.8));

  fesetround(FE_TOWARDZERO);
  printf ("FE_TOWARDZERO: %.1f\n", rint(-3.8));

  fesetround(FE_UPWARD);
  printf ("FE_UPWARD: %.1f\n", rint(-3.8));
  return 0;
}

output:

rounding -3.8:
FE_DOWNWARD: -4.0
FE_TONEAREST: -4.0
FE_TOWARDZERO: -3.0
FE_UPWARD: -3.0

7. fegetenv 函数

int fegetenv (fenv_t* envp);

函数介绍:试图在envp所指向的对象中存储浮点环境的当前状态。浮点环境是一组影响浮点计算的状态标志和控制模式(包括浮点异常和舍入方向模式)。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

fenv_t* envp:指向存储浮点环境状态的fenv_t对象的指针。

返回值:返回零,如果状态存储成功。否则,返回非零。

8. fesetenv 函数

int fesetenv (const fenv_t* envp);

函数介绍:试图建立由envp所指向的对象表示的浮点环境的状态。浮点环境是一组影响浮点计算的状态标志和控制模式(包括浮点异常和舍入方向模式)。如果成功,函数将更改浮点环境的当前状态,而不会实际引发在这种状态中指定的异常。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

const fenv_t* envp:一个指向fenv_t值的指针(由之前对fegetenv或feholdexcept的调用填充),或者一个浮点环境宏值:

FE_DFL_ENV Default floating-point environment (the same as at program startup).

返回值:返回零,如果状态成功建立。否则返回非零。

9. feholdexcept 函数

int feholdexcept (fenv_t* envp);

函数介绍:保存 envp 所指向对象中的浮点环境的当前状态。然后,它会重置当前状态,并且——如果支持的话——将环境设置为不间断模式。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

fenv_t* envp:指向存储浮点环境状态的fenv_t对象的指针。

返回值:返回零,如果功能成功完成,包括设置浮点环境为不停模式。

/* feholdexcept/feupdateenv example */
#include <stdio.h>      /* printf, puts */
#include <fenv.h>       /* feholdexcept, feclearexcept, fetestexcept, feupdateenv, FE_* */
#include <math.h>       /* log */
#pragma STDC FENV_ACCESS on

double log_zerook (double x) {
  fenv_t fe;
  feholdexcept(&fe);
  x=log(x);
  feclearexcept (FE_OVERFLOW|FE_DIVBYZERO);
  feupdateenv(&fe);
  return x;
}

int main ()
{
  feclearexcept (FE_ALL_EXCEPT);
  printf ("log(0.0): %f\n", log_zerook(0.0));
  if (!fetestexcept(FE_ALL_EXCEPT))
    puts ("no exceptions raised");
  return 0;
}

output:

log(0.0): -inf
no exceptions raised

10. feupdateenv 函数

int feupdateenv (const fenv_t* envp);

函数介绍:试图建立由envp所指向的对象表示的浮点环境的状态。然后尝试在调用之前在浮点环境中设置异常。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

const fenv_t* envp:一个指向fenv_t值的指针(由之前对fegetenv或feholdexcept的调用填充),或者一个浮点环境宏值:

FE_DFL_ENV Default floating-point environment (the same as at program startup).

返回值:成功返回 0 ;否则,返回非零

/* feholdexcept/feupdateenv example */
#include <stdio.h>      /* printf, puts */
#include <fenv.h>       /* feholdexcept, feclearexcept, fetestexcept, feupdateenv, FE_* */
#include <math.h>       /* log */
#pragma STDC FENV_ACCESS on

double log_zerook (double x) 
{
  fenv_t fe;
  feholdexcept(&fe);
  x=log(x);
  feclearexcept (FE_OVERFLOW|FE_DIVBYZERO);
  feupdateenv(&fe);
  return x;
}

int main ()
{
  feclearexcept (FE_ALL_EXCEPT);
  printf ("log(0.0): %f\n", log_zerook(0.0));
  if (!fetestexcept(FE_ALL_EXCEPT))
    puts ("no exceptions raised");
  return 0;
}

output:

log(0.0): -inf
no exceptions raised

11. fetestexcept 函数

int fetestexcept (int excepts);

函数介绍:返回当前设置的异常,其中包括excepts指定的异常。返回的值是浮点环境中当前设置的异常子集的位或表示形式。或者是零,如果异常中的任何异常都没有当前设置。

注意:调用此函数的程序应确保为调用启用了编译指示FENV_ACCESS。

参数:

int excepts:位掩码值:实现支持的任意数量的浮点异常值的组合(带有按位OR):

FE_DIVBYZERO Pole error: division by zero, or some other asymptotically infinite result (from finite arguments).
FE_INEXACT Inexact: the result is not exact.
FE_INVALID Domain error: At least one of the arguments is a value for which the function is not defined.
FE_OVERFLOW Overflow range error: The result is too large in magnitude to be represented as a value of the return type.
FE_UNDERFLOW Underflow range error: The result is too small in magnitude to be represented as a value of the return type.
FE_ALL_EXCEPT All exceptions (selects all of the exceptions supported by the implementation).

返回值:返回零,如果异常中的任何异常都没有被设置;否则,返回非零值。

/* fetestexcept example */
#include <stdio.h>      /* puts */
#include <fenv.h>       /* feraiseexcept, fetestexcept, FE_* */
#pragma STDC FENV_ACCESS on

double fn (double x) {
  /* some function for which zero is a domain and range error */
  if (x==0.0) feraiseexcept(FE_INVALID|FE_OVERFLOW);
  return x;
}

int main ()
{
  int fe;

  feclearexcept (FE_ALL_EXCEPT);
  fn (0.0);

  /* testing for single exception: */
  if (fetestexcept(FE_OVERFLOW)) puts ("FE_OVERFLOW is set");

  /* testing multiple exceptions: */
  fe = fetestexcept (FE_ALL_EXCEPT);

  puts ("The following exceptions are set:");
  if (fe & FE_DIVBYZERO) puts ("FE_DIVBYZERO");
  if (fe & FE_INEXACT)   puts ("FE_INEXACT");
  if (fe & FE_INVALID)   puts ("FE_INVALID");
  if (fe & FE_OVERFLOW)  puts ("FE_OVERFLOW");
  if (fe & FE_UNDERFLOW) puts ("FE_UNDERFLOW");

  return 0;
}

output:


FE_OVERFLOW is set
The following exceptions are set:
FE_INVALID
FE_OVERFLOW

猜你喜欢

转载自blog.csdn.net/thecentry/article/details/82049148
今日推荐