-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloatp10.cpp
More file actions
118 lines (113 loc) · 3 KB
/
floatp10.cpp
File metadata and controls
118 lines (113 loc) · 3 KB
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
117
118
/*---------------------------------------------------------------------------*/
/* _floatp10() get the characteristic, mantissa and sign of a double */
/* precision floating point number such that: */
/* */
/* 1 <= mantissa < 10 */
/* - 308 <= characteristic <= 308 */
/* */
/* negative: false if positive */
/* true if negative */
/*---------------------------------------------------------------------------*/
#include <math.h>
int _floatp10(double *fnum, bool *negative, int prec)
{
int i;
int fpower = 0;
int ipower = 256;
int rpower = 256;
double fround = 0.5;
static double const pfpower[] =
{
1.0e+256,
1.0e+128,
1.0e+64,
1.0e+32,
1.0e+16,
1.0e+8,
1.0e+4,
1.0e+2,
1.0e+1
};
static double const nfpower[] =
{
1.0e-256,
1.0e-128,
1.0e-64,
1.0e-32,
1.0e-16,
1.0e-8,
1.0e-4,
1.0e-2,
1.0e-1
};
*negative = *fnum < (double)0;
if(*fnum != 0.0)
{
if(prec > 0)
{
if(prec < 309)
{
for(i = 0; i < 9; i++)
{
if(prec >= rpower)
{
fround /= pfpower[i];
prec -= rpower;
}
rpower >>= 1;
}
}
else
fround = (double)0;
}
else if(prec < 0)
{
if(prec > -310)
{
fround = (double)5;
for(i = 0; i < 9; i++)
{
if(prec >= rpower)
{
fround *= pfpower[i];
prec += rpower;
}
rpower >>= 1;
}
}
else
fround = (double)0;
}
*fnum = fabs(*fnum) + fround;
if(*fnum < (double)1)
{
for(i = 0; i < 9; i++)
{
if(*fnum <= nfpower[i])
{
*fnum *= pfpower[i];
fpower -= ipower;
}
ipower >>= 1;
}
}
else if(*fnum >= (double)10)
{
for(i = 0; i < 9; i++)
{
if(*fnum >= pfpower[i])
{
*fnum /= pfpower[i];
fpower += ipower;
}
ipower >>= 1;
}
}
if(*fnum < (double)1)
{
*fnum *= pfpower[8];
fpower--;
}
}
return fpower;
}