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

📄 s_logarithm.c

📁 Newlib 嵌入式 C库 标准实现代码
💻 C
字号:
/* @(#)z_logarithm.c 1.0 98/08/13 *//****************************************************************** * The following routines are coded directly from the algorithms * and coefficients given in "Software Manual for the Elementary * Functions" by William J. Cody, Jr. and William Waite, Prentice * Hall, 1980. ******************************************************************//*FUNCTION       <<log>>, <<logf>>, <<log10>>, <<log10f>>, <<logarithm>>, <<logarithmf>>---natural or base 10 logarithmsINDEX    logINDEX    logfINDEX    log10INDEX    log10fANSI_SYNOPSIS       #include <math.h>       double log(double <[x]>);       float logf(float <[x]>);       double log10(double <[x]>);       float log10f(float <[x]>);TRAD_SYNOPSIS       #include <math.h>       double log(<[x]>);       double <[x]>;       float logf(<[x]>);       float <[x]>;       double log10(<[x]>);       double <[x]>;       float log10f(<[x]>);       float <[x]>;DESCRIPTIONReturn the natural or base 10 logarithm of <[x]>, that is, its logarithm base e(where e is the base of the natural system of logarithms, 2.71828@dots{}) orbase 10.<<log>> and <<logf>> are identical save for the return and argument types.<<log10>> and <<log10f>> are identical save for the return and argument types.RETURNSNormally, returns the calculated value.  When <[x]> is zero, thereturned value is <<-HUGE_VAL>> and <<errno>> is set to <<ERANGE>>.When <[x]> is negative, the returned value is <<-HUGE_VAL>> and<<errno>> is set to <<EDOM>>.  You can control the error behavior via<<matherr>>.PORTABILITY<<log>> is ANSI. <<logf>> is an extension.<<log10>> is ANSI. <<log10f>> is an extension.*//****************************************************************** * Logarithm * * Input: *   x - floating point value *   ten - indicates base ten numbers * * Output: *   logarithm of x * * Description: *   This routine calculates logarithms. * *****************************************************************/#include "fdlibm.h"#include "zmath.h"#ifndef _DOUBLE_IS_32BITSstatic const double a[] = { -0.64124943423745581147e+02,                             0.16383943563021534222e+02,                            -0.78956112887481257267 };static const double b[] = { -0.76949932108494879777e+03,                             0.31203222091924532844e+03,                            -0.35667977739034646171e+02 };static const double C1 =  22713.0 / 32768.0;static const double C2 =  1.428606820309417232e-06;static const double C3 =  0.43429448190325182765;double_DEFUN (logarithm, (double, int),        double x _AND        int ten){  int N;  double f, w, z;  /* Check for range and domain errors here. */  if (x == 0.0)    {      errno = ERANGE;      return (-z_infinity.d);    }  else if (x < 0.0)    {      errno = EDOM;      return (z_notanum.d);    }  else if (!isfinite(x))    {      if (isnan(x))        return (z_notanum.d);      else        return (z_infinity.d);    }  /* Get the exponent and mantissa where x = f * 2^N. */  f = frexp (x, &N);  z = f - 0.5;  if (f > __SQRT_HALF)    z = (z - 0.5) / (f * 0.5 + 0.5);  else    {      N--;      z /= (z * 0.5 + 0.5);    }  w = z * z;  /* Use Newton's method with 4 terms. */  z += z * w * ((a[2] * w + a[1]) * w + a[0]) / (((w + b[2]) * w + b[1]) * w + b[0]);  if (N != 0)    z = (N * C2 + z) + N * C1;  if (ten)    z *= C3;  return (z);}#endif /* _DOUBLE_IS_32BITS */

⌨️ 快捷键说明

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