PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Simple Libraries
TextDevices
libsimpletext
floatToString.c
1
/*
2
* @file floatToString.c
3
*
4
* Copyright (c) 2013 Parallax Inc.
5
* Author Andy Lindsay
6
* Clamp to 6 decimal digits by Steve - buffer must be at least 10 chars.
7
*
8
* See end of file for terms of use.
9
*/
10
11
#include "
simpletext.h
"
12
13
#ifndef S_ISNAN
14
#define S_ISNAN(x) (x != x)
15
#endif
/* !defined(S_ISNAN) */
16
#ifndef S_ISINF
17
#define S_ISINF(x) (x != 0.0 && x + x == x)
18
#endif
/* !defined(S_ISINF) */
19
20
char
* float2string(
float
f,
char
*s,
int
ccount,
int
digits)
21
{
22
union
convert {
23
float
v;
24
int
w;
25
} fval;
26
27
int
m = 0;
28
int
k = 0;
29
30
int
j = 0;
31
int
sign = 0;
32
int
g = 0;
33
34
int
reps = 0;
35
float
scale = 0.0;
36
int
ctr = 0;
37
38
int
offset;
39
int
p;
40
int
q;
41
int
n;
42
43
if
(S_ISNAN(f)) {
44
strcpy(s,
"nan"
);
45
return
s;
46
}
47
if
(S_ISINF(f)) {
48
if
(((
int
)f) & 0x80000000)
49
strcpy(s,
"-inf"
);
50
else
51
strcpy(s,
"inf"
);
52
return
s;
53
}
54
55
/* clamp the digits. */
56
int
clamp = 6;
/* a buffer must be at least clamp + 4 digits */
57
digits = (digits > clamp) ? clamp : digits;
58
59
if
(f < 0.0)
60
{
61
sign = 1;
62
f = -f;
63
}
64
65
if
(sign)
66
{
67
s[j++] =
'-'
;
68
}
69
70
/* Find resonable starting value for scale.
71
// Using 2^10x has similar values to 10^3x. */
72
fval.v = f;
73
g = fval.w;
74
75
g >>= 23;
76
g &= 0xFF;
77
g -= 127;
78
79
reps = (g / 10);
80
scale = 1.0;
81
for
(ctr = 0; ctr <= reps; ctr++)
82
{
83
scale *= 1000.0;
84
}
85
86
/* If integer is zero, 0 */
87
if
(f < 1.0)
88
{
89
s[j++] =
'0'
;
90
}
91
else
92
{
93
char
c =
'0'
;
94
for
( ; scale >= 1.0; scale /= 10.0)
95
{
96
if
(f >= scale)
97
{
98
break
;
99
}
100
}
101
for
( ; scale >= 1.0; scale /= 10.0)
102
{
103
c = (
char
)(f/scale);
104
f -= ((
float
)c*scale);
105
c += 48;
106
s[j++] = c;
107
}
108
}
109
110
/* If digits > current size, move right, then pad with spaces
111
// if(digits < 0) digits = 0; */
112
offset = ccount - j - digits - 1;
113
if
(digits == 0)
114
offset++;
115
116
p = j + offset;
117
q = j;
118
n = p;
119
120
if
(offset > 0)
121
{
122
for
( ; j >= 0; )
123
{
124
s[n--] = s[j--];
125
}
126
for
( ; n >= 0; )
127
{
128
s[n--] =
' '
;
129
}
130
j = p;
131
}
132
else
133
{
134
j = q;
135
}
136
137
/* Append with fractional */
138
if
(digits>0) s[j++] =
'.'
;
139
140
k = j;
141
k += digits;
142
for
( ; j <= k; )
143
{
144
f *= 10.0;
145
s[j++] = (
char
)f +
'0'
;
146
f -= ((int) f);
147
}
148
149
m = j-1;
150
j--;
151
if
(s[j] >=
'5'
)
152
{
153
j--;
154
for
( ; (j >= 0); j--)
155
{
156
if
((s[j] <
'0'
)||(s[j] >
'9'
))
continue
;
157
if
(s[j] <
'9'
)
158
{
159
s[j]++;
160
break
;
161
}
162
else
163
{
164
s[j] =
'0'
;
165
}
166
}
167
}
168
169
s[m] = 0;
170
return
s;
171
}
172
173
#if 0
174
char
* float2string(
float
f,
char
*s,
int
ccount,
int
digits)
175
{
176
union
convert {
177
float
v;
178
int
w;
179
} fval;
180
181
int
m = 0;
182
int
k = 0;
183
184
int
j = 0;
185
int
sign = 0;
186
int
g = 0;
187
188
int
reps = 0;
189
float
scale = 0.0;
190
int
ctr = 0;
191
192
int
offset;
193
int
p;
194
int
q;
195
int
n;
196
197
/* clamp the digits. */
198
int
clamp = 6;
/* a buffer must be at least clamp + 4 digits */
199
digits = (digits > clamp) ? clamp : digits;
200
201
if
(f < 0.0)
202
{
203
sign = 1;
204
f = -f;
205
}
206
207
if
(sign)
208
{
209
s[j++] =
'-'
;
210
}
211
212
/* Find resonable starting value for scale.
213
// Using 2^10x has similar values to 10^3x. */
214
fval.v = f;
215
g = fval.w;
216
217
g >>= 23;
218
g &= 0xFF;
219
g -= 127;
220
221
reps = (g / 10);
222
scale = 1.0;
223
for
(ctr = 0; ctr <= reps; ctr++)
224
{
225
scale *= 1000.0;
226
}
227
228
/* If integer is zero, 0 */
229
if
(f < 1.0)
230
{
231
s[j++] =
'0'
;
232
}
233
else
234
{
235
int
started = 0;
236
char
c =
'0'
;
237
for
( ; scale >= 1.0; scale /= 10.0)
238
{
239
if
(f >= scale)
240
{
241
c = (
char
)(f/scale);
242
f -= ((
float
)c*scale);
243
c += 48;
244
s[j++] = c;
245
started++;
246
}
247
else
if
(started) {
248
s[j++] =
'0'
;
249
}
250
}
251
}
252
253
/* If digits > current size, move right, then pad with spaces
254
// if(digits < 0) digits = 0; */
255
offset = ccount - j - digits - 1;
256
if
(digits == 0)
257
offset++;
258
259
p = j + offset;
260
q = j;
261
n = p;
262
263
if
(offset > 0)
264
{
265
for
( ; j >= 0; )
266
{
267
s[n--] = s[j--];
268
}
269
for
( ; n >= 0; )
270
{
271
s[n--] =
' '
;
272
}
273
j = p;
274
}
275
else
276
{
277
j = q;
278
}
279
280
/* Append with fractional */
281
if
(digits>0) s[j++] =
'.'
;
282
283
k = j;
284
k += digits;
285
for
( ; j <= k; )
286
{
287
f *= 10.0;
288
s[j++] = (
char
)f +
'0'
;
289
f -= ((int) f);
290
}
291
292
m = j-1;
293
j--;
294
if
(s[j] >=
'5'
)
295
{
296
j--;
297
for
( ; (j >= 0); j--)
298
{
299
if
((s[j] <
'0'
)||(s[j] >
'9'
))
continue
;
300
if
(s[j] <
'9'
)
301
{
302
s[j]++;
303
break
;
304
}
305
else
306
{
307
s[j] =
'0'
;
308
}
309
}
310
}
311
312
s[m] = 0;
313
return
s;
314
}
315
#endif
316
317
/*
318
+--------------------------------------------------------------------
319
| TERMS OF USE: MIT License
320
+--------------------------------------------------------------------
321
Permission is hereby granted, free of charge, to any person obtaining
322
a copy of this software and associated documentation files
323
(the "Software"), to deal in the Software without restriction,
324
including without limitation the rights to use, copy, modify, merge,
325
publish, distribute, sublicense, and/or sell copies of the Software,
326
and to permit persons to whom the Software is furnished to do so,
327
subject to the following conditions:
328
329
The above copyright notice and this permission notice shall be
330
included in all copies or substantial portions of the Software.
331
332
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
333
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
334
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
335
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
336
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
337
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
338
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
339
+--------------------------------------------------------------------
340
*/
float
simpletext.h
This library provides a collection of functions for communicating with text devices such as SimpleIDE...
char
Generated on Fri May 20 2022 16:48:05 for PropWare by
1.8.17