summaryrefslogtreecommitdiff
path: root/libm/float/exp2f.c
blob: 0de21decdcabbc157837eedc9f6700afc875b973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*							exp2f.c
 *
 *	Base 2 exponential function
 *
 *
 *
 * SYNOPSIS:
 *
 * float x, y, exp2f();
 *
 * y = exp2f( x );
 *
 *
 *
 * DESCRIPTION:
 *
 * Returns 2 raised to the x power.
 *
 * Range reduction is accomplished by separating the argument
 * into an integer k and fraction f such that
 *     x    k  f
 *    2  = 2  2.
 *
 * A polynomial approximates 2**x in the basic range [-0.5, 0.5].
 *
 *
 * ACCURACY:
 *
 *                      Relative error:
 * arithmetic   domain     # trials      peak         rms
 *    IEEE     -127,+127    100000      1.7e-7      2.8e-8
 *
 *
 * See exp.c for comments on error amplification.
 *
 *
 * ERROR MESSAGES:
 *
 *   message         condition      value returned
 * exp underflow    x < -MAXL2        0.0
 * exp overflow     x > MAXL2         MAXNUMF
 *
 * For IEEE arithmetic, MAXL2 = 127.
 */


/*
Cephes Math Library Release 2.2:  June, 1992
Copyright 1984, 1987, 1988, 1992 by Stephen L. Moshier
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
*/



#include <math.h>
static char fname[] = {"exp2f"};

static float P[] = {
 1.535336188319500E-004,
 1.339887440266574E-003,
 9.618437357674640E-003,
 5.550332471162809E-002,
 2.402264791363012E-001,
 6.931472028550421E-001
};
#define MAXL2 127.0
#define MINL2 -127.0



extern float MAXNUMF;

float polevlf(float, float *, int), floorf(float), ldexpf(float, int);

float exp2f( float xx )
{
float x, px;
int i0;

x = xx;
if( x > MAXL2)
	{
	mtherr( fname, OVERFLOW );
	return( MAXNUMF );
	}

if( x < MINL2 )
	{
	mtherr( fname, UNDERFLOW );
	return(0.0);
	}

/* The following is necessary because range reduction blows up: */
if( x == 0 )
	return(1.0);

/* separate into integer and fractional parts */
px = floorf(x);
i0 = px;
x = x - px;

if( x > 0.5 )
	{
	i0 += 1;
	x -= 1.0;
	}

/* rational approximation
 * exp2(x) = 1.0 +  xP(x)
 */
px = 1.0 + x * polevlf( x, P, 5 );

/* scale by power of 2 */
px = ldexpf( px, i0 );
return(px);
}