2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "build/build_config.h"
23 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
25 void uli2a(unsigned long int num
, unsigned int base
, int uc
, char *bf
)
29 while (num
/ d
>= base
)
34 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
43 void li2a(long num
, char *bf
)
49 uli2a(num
, 10, 0, bf
);
54 void ui2a(unsigned int num
, unsigned int base
, int uc
, char *bf
)
58 while (num
/ d
>= base
)
63 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
72 void i2a(int num
, char *bf
)
83 if (ch
>= '0' && ch
<= '9')
85 else if (ch
>= 'a' && ch
<= 'f')
87 else if (ch
>= 'A' && ch
<= 'F')
93 char a2i(char ch
, const char **src
, int base
, int *nump
)
98 while ((digit
= a2d(ch
)) >= 0) {
101 num
= num
* base
+ digit
;
109 #ifndef HAVE_ITOA_FUNCTION
112 ** The following two functions together make up an itoa()
113 ** implementation. Function i2a() is a 'private' function
114 ** called by the public itoa() function.
116 ** itoa() takes three arguments:
117 ** 1) the integer to be converted,
118 ** 2) a pointer to a character conversion buffer,
119 ** 3) the radix for the conversion
120 ** which can range between 2 and 36 inclusive
121 ** range errors on the radix default it to base10
122 ** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
125 static char *_i2a(unsigned i
, char *a
, unsigned base
)
128 a
= _i2a(i
/ base
, a
, base
);
129 *a
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i
% base
];
133 char *itoa(int i
, char *a
, int base
)
135 if ((base
< 2) || (base
> 36))
139 *_i2a(-(unsigned) i
, a
+ 1, base
) = 0;
141 *_i2a(i
, a
, base
) = 0;
147 char *ftoa(float x
, char *floatString
)
151 char intString2
[12] = { 0, };
152 char *decimalPoint
= ".";
155 if (x
> 0) // Rounding for x.xxx display format
160 value
= (int32_t)(x
* 1000.0f
); // Convert float * 1000 to an integer
162 itoa(ABS(value
), intString1
, 10); // Create string from abs of integer value
165 intString2
[0] = ' '; // Positive number, add a pad space
167 intString2
[0] = '-'; // Negative number, add a negative sign
169 if (strlen(intString1
) == 1) {
173 strcat(intString2
, intString1
);
174 } else if (strlen(intString1
) == 2) {
177 strcat(intString2
, intString1
);
178 } else if (strlen(intString1
) == 3) {
180 strcat(intString2
, intString1
);
182 strcat(intString2
, intString1
);
185 dpLocation
= strlen(intString2
) - 3;
187 strncpy(floatString
, intString2
, dpLocation
);
188 floatString
[dpLocation
] = '\0';
189 strcat(floatString
, decimalPoint
);
190 strcat(floatString
, intString2
+ dpLocation
);
195 // Simple and fast atof (ascii to float) function.
197 // - Executes about 5x faster than standard MSCRT library atof().
198 // - An attractive alternative if the number of calls is in the millions.
199 // - Assumes input is a proper integer, fraction, or scientific format.
200 // - Matches library atof() to 15 digits (except at extreme exponents).
201 // - Follows atof() precedent of essentially no error checking.
203 // 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
204 #define white_space(c) ((c) == ' ' || (c) == '\t')
205 #define valid_digit(c) ((c) >= '0' && (c) <= '9')
206 float fastA2F(const char *p
)
209 float sign
, value
, scale
;
211 // Skip leading white space, if any.
212 while (white_space(*p
)) {
222 } else if (*p
== '+') {
226 // Get digits before decimal point or exponent, if any.
228 while (valid_digit(*p
)) {
229 value
= value
* 10.0f
+ (*p
- '0');
233 // Get digits after decimal point, if any.
238 while (valid_digit(*p
)) {
239 value
+= (*p
- '0') / pow10
;
245 // Handle exponent, if any.
247 if ((*p
== 'e') || (*p
== 'E')) {
251 // Get sign of exponent, if any.
257 } else if (*p
== '+') {
261 // Get digits of exponent, if any.
263 while (valid_digit(*p
)) {
264 expon
= expon
* 10 + (*p
- '0');
270 // Calculate scaling factor.
271 // while (expon >= 50) { scale *= 1E50f; expon -= 50; }
282 // Return signed and scaled floating point result.
283 return sign
* (frac
? (value
/ scale
) : (value
* scale
));