⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dblround.how

📁 C语言库函数的源代码,是C语言学习参考的好文档。
💻 HOW
字号:
+++Date last modified: 05-Jul-1997

  Let's look at DBLROUND.C without the #include's or conditional macros for 
NCEG...


double dround(double x)
{
      Boolean_T flag;
      static volatile double X;

      SAVEROUND();
      if (True_ == (flag = (x < 0.0)))
            X = -x;
      else  X = x;
      X += 1.0 / DBL_EPSILON;
      X -= 1.0 / DBL_EPSILON;
      RESTOREROUND();
      return ((flag) ? -X : X);
}


  The `flag' is obviously there just to keep track of whether `x' is positive 
or negative.

  The key to the algorithm is the reciprocal of DBL_EPSILON. Since
DBL_EPSILON is the smallest fractional number which can be represented, its
reciprocal is therefore the smallest number that cannot have a fractional
part. Once you add this reciprocal to `x', its fractional part is stripped
off. Simply subtracting the reciprocal back out returns `x' without its
fractional component.

  Simple, clever, and elegant - thanks to Ross Cottrell, the original author.
The additional features added for SNIPPETS include:

1.  The SAVEROUND and RESTOREROUND macros are provided for compilers which
    support the NCEG floating point extensions and provide that for the
    duration of the function, rounding will be performed to the nearest
    value.

2.  An intermediate variable X, declared volatile, is used rather than simply
    using the passed variable x in order to prevent some smart compiler
    optimizer from optimizing the function into oblivion. It's also declared
    static since some compilers will not honor the volatile keyword for an
    auto (stack-based) variable. Since X is declared static, dround() is not
    re-entrant.

  All PC compiler vendors use IEEE 754-1985 for their standard floating-point
format. You can get a copy of every standard ever published by IEEE, ANSI,
ISO, API, ASTM, and every other standards organization you can name from
Global Engineering Documents. Be forewarned, however, that they're not cheap.
I think the cheapest standard document I ever got from them was over $50.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -