Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      fegetenv, fesetenv

      From cppreference.com
      <c‎ |numeric‎ |fenv
       
       
       
       
      Defined in header<fenv.h>
      int fegetenv( fenv_t* envp);
      (1)(since C99)
      int fesetenv(const fenv_t* envp);
      (2)(since C99)

      1) Attempts to store the status of the floating-point environment in the object pointed to byenvp.

      2) Attempts to establish the floating-point environment from the object pointed to byenvp. The value of that object must be previously obtained by a call tofeholdexcept orfegetenv or be a floating-point macro constant. If any of the floating-point status flags are set inenvp, they become set in the environment (and are then testable withfetestexcept), but the corresponding floating-point exceptions are not raised (execution continues uninterrupted)

      Contents

      [edit]Parameters

      envp - pointer to the object of typefenv_t which holds the status of the floating-point environment

      [edit]Return value

      0 on success, non-zero otherwise.

      [edit]Example

      Run this code
      #include <stdio.h>#include <math.h>#include <fenv.h> #pragma STDC FENV_ACCESS ON void show_fe_exceptions(void){printf("current exceptions raised: ");if(fetestexcept(FE_DIVBYZERO))printf(" FE_DIVBYZERO");if(fetestexcept(FE_INEXACT))printf(" FE_INEXACT");if(fetestexcept(FE_INVALID))printf(" FE_INVALID");if(fetestexcept(FE_OVERFLOW))printf(" FE_OVERFLOW");if(fetestexcept(FE_UNDERFLOW))printf(" FE_UNDERFLOW");if(fetestexcept(FE_ALL_EXCEPT)==0)printf(" none");printf("\n");} void show_fe_rounding_method(void){printf("current rounding method:    ");switch(fegetround()){caseFE_TONEAREST:printf("FE_TONEAREST");break;caseFE_DOWNWARD:printf("FE_DOWNWARD");break;caseFE_UPWARD:printf("FE_UPWARD");break;caseFE_TOWARDZERO:printf("FE_TOWARDZERO");break;default:printf("unknown");};printf("\n");} void show_fe_environment(void){    show_fe_exceptions();    show_fe_rounding_method();}     int main(void){    fenv_t curr_env;int rtn; /* Show default environment. */    show_fe_environment();printf("\n"); /* Perform some computation under default environment. */printf("+11.5 -> %+4.1f\n",rint(+11.5));/* midway between two integers */printf("+12.5 -> %+4.1f\n",rint(+12.5));/* midway between two integers */    show_fe_environment();printf("\n"); /* Save current environment. */    rtn= fegetenv(&curr_env); /* Perform some computation with new rounding method. */feclearexcept(FE_ALL_EXCEPT);fesetround(FE_DOWNWARD);printf("1.0/0.0 = %f\n",1.0/0.0);printf("+11.5 -> %+4.1f\n",rint(+11.5));printf("+12.5 -> %+4.1f\n",rint(+12.5));    show_fe_environment();printf("\n"); /* Restore previous environment. */    rtn= fesetenv(&curr_env);    show_fe_environment(); return0;}

      Output:

      current exceptions raised: nonecurrent rounding method:   FE_TONEAREST +11.5 -> +12.0+12.5 -> +12.0current exceptions raised: FE_INEXACTcurrent rounding method:   FE_TONEAREST 1.0/0.0 = inf+11.5 -> +11.0+12.5 -> +12.0current exceptions raised: FE_DIVBYZERO FE_INEXACTcurrent rounding method:   FE_DOWNWARD current exceptions raised: FE_INEXACTcurrent rounding method:   FE_TONEAREST

      [edit]References

      • C11 standard (ISO/IEC 9899:2011):
      • 7.6.4.1 The fegetenv function (p: 213)
      • 7.6.4.3 The fesetenv function (p: 214)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.6.4.1 The fegetenv function (p: 194)
      • 7.6.4.3 The fesetenv function (p: 195)

      [edit]See also

      saves the environment, clears all status flags and ignores all future errors
      (function)[edit]
      restores the floating-point environment and raises the previously raise exceptions
      (function)[edit]
      default floating-point environment
      (macro constant)[edit]
      C++ documentation forfegetenv,fesetenv
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/fenv/feenv&oldid=133798"

      [8]ページ先頭

      ©2009-2025 Movatter.jp