amath  1.8.5
Simple command line calculator
log10.c File Reference

Base 10 logarithm function. More...

#include "prim.h"
Include dependency graph for log10.c:

Go to the source code of this file.

Functions

double log10 (double x)
 Base 10 logarithm function. More...
 

Variables

static const double two54 = 1.80143985094819840000e+16
 
static const double ivln10 = 4.34294481903251816668e-01
 
static const double log10_2hi = 3.01029995663611771306e-01
 
static const double log10_2lo = 3.69423907715893078616e-13
 
static double zero = 0.0
 

Detailed Description

Base 10 logarithm function.

Definition in file log10.c.

Function Documentation

◆ log10()

double log10 ( double  x)

Base 10 logarithm function.

Method:
Let log10_2hi = leading 40 bits of log10(2) and
    log10_2lo = log10(2) - log10_2hi,
    ivln10    = 1/log(10) rounded.
Then
    n = ilogb(x),
    if(n<0)  n = n+1;
    x = scalbn(x,-n);
    log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x))

Note 1:
To guarantee log10(10**n)=n, where 10**n is normal, the rounding
mode must set to Round-to-Nearest.

Note 2:
[1/log(10)] rounded to 53 bits has error .198 ulps;
log10 is monotonic at all binary break points.
Special cases:
log10(x) is NaN with signal if x < 0;
log10(+INF) is +INF with no signal; log10(0) is -INF with signal;
log10(NaN) is that NaN with no signal;
log10(10**N) = N  for N=0,1,...,22.
Constants:
The hexadecimal values are the intended ones for the following constants.
The decimal values may be used, provided that the compiler will convert
from decimal to binary accurately enough to produce the hexadecimal values
shown.

Definition at line 93 of file log10.c.

References ivln10, log(), log10_2hi, log10_2lo, two54, and zero.

Referenced by DecimalSystem::GetText(), and RealNumber::Log10().

94 {
95  double y,z;
96  int32_t i,k,hx;
97  uint32_t lx;
98 
99  EXTRACT_WORDS(hx,lx,x);
100 
101  k=0;
102  if (hx < 0x00100000) { /* x < 2**-1022 */
103  if (((hx&0x7fffffff)|lx)==0)
104  return -two54/zero; /* log(+-0)=-inf */
105  if (hx<0) return (x-x)/zero; /* log(-#) = NaN */
106  k -= 54;
107  x *= two54; /* subnormal number, scale up x */
108  GET_HIGH_WORD(hx, x); /* high word of x */
109  }
110  if (hx >= 0x7ff00000) return x+x;
111  k += (hx>>20)-1023;
112  i = ((uint32_t)k&0x80000000)>>31;
113  hx = (hx&0x000fffff)|((0x3ff-i)<<20);
114  y = (double)(k+i);
115  SET_HIGH_WORD(x,hx);
116  z = y*log10_2lo + ivln10*log(x);
117  return z+y*log10_2hi;
118 }
static double zero
Definition: log10.c:54
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
#define EXTRACT_WORDS(ix0, ix1, d)
Get two 32 bit ints from a double.
Definition: prim.h:156
static const double two54
Definition: log10.c:49
static const double log10_2hi
Definition: log10.c:51
static const double ivln10
Definition: log10.c:50
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198
double log(double x)
Natural logarithm function (base e)
Definition: log.c:109
static const double log10_2lo
Definition: log10.c:52
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ivln10

const double ivln10 = 4.34294481903251816668e-01
static

Definition at line 50 of file log10.c.

Referenced by log10().

◆ log10_2hi

const double log10_2hi = 3.01029995663611771306e-01
static

Definition at line 51 of file log10.c.

Referenced by log10().

◆ log10_2lo

const double log10_2lo = 3.69423907715893078616e-13
static

Definition at line 52 of file log10.c.

Referenced by log10().

◆ two54

const double two54 = 1.80143985094819840000e+16
static

Definition at line 49 of file log10.c.

Referenced by log10().

◆ zero

double zero = 0.0
static

Definition at line 54 of file log10.c.

Referenced by log10().