2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
28 #include "build/build_config.h"
31 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
33 void uli2a(unsigned long int num
, unsigned int base
, int uc
, char *bf
)
37 while (num
/ d
>= base
)
42 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
51 void li2a(long num
, char *bf
)
57 uli2a(num
, 10, 0, bf
);
62 void ui2a(unsigned int num
, unsigned int base
, int uc
, char *bf
)
66 while (num
/ d
>= base
)
71 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
80 void i2a(int num
, char *bf
)
91 if (ch
>= '0' && ch
<= '9')
93 else if (ch
>= 'a' && ch
<= 'f')
95 else if (ch
>= 'A' && ch
<= 'F')
101 char a2i(char ch
, const char **src
, int base
, int *nump
)
103 const char *p
= *src
;
106 while ((digit
= a2d(ch
)) >= 0) {
109 num
= num
* base
+ digit
;
117 #ifndef HAVE_ITOA_FUNCTION
120 ** The following two functions together make up an itoa()
121 ** implementation. Function i2a() is a 'private' function
122 ** called by the public itoa() function.
124 ** itoa() takes three arguments:
125 ** 1) the integer to be converted,
126 ** 2) a pointer to a character conversion buffer,
127 ** 3) the radix for the conversion
128 ** which can range between 2 and 36 inclusive
129 ** range errors on the radix default it to base10
130 ** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
133 static char *_i2a(unsigned i
, char *a
, unsigned base
)
136 a
= _i2a(i
/ base
, a
, base
);
137 *a
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i
% base
];
141 char *itoa(int i
, char *a
, int base
)
143 if ((base
< 2) || (base
> 36))
147 *_i2a(-(unsigned) i
, a
+ 1, base
) = 0;
149 *_i2a(i
, a
, base
) = 0;
155 char *ftoa(float x
, char *floatString
)
159 char intString2
[12] = { 0, };
160 char *decimalPoint
= ".";
163 if (x
> 0) // Rounding for x.xxx display format
168 value
= (int32_t)(x
* 1000.0f
); // Convert float * 1000 to an integer
170 itoa(abs(value
), intString1
, 10); // Create string from abs of integer value
173 intString2
[0] = ' '; // Positive number, add a pad space
175 intString2
[0] = '-'; // Negative number, add a negative sign
177 if (strlen(intString1
) == 1) {
181 strcat(intString2
, intString1
);
182 } else if (strlen(intString1
) == 2) {
185 strcat(intString2
, intString1
);
186 } else if (strlen(intString1
) == 3) {
188 strcat(intString2
, intString1
);
190 strcat(intString2
, intString1
);
193 dpLocation
= strlen(intString2
) - 3;
195 memcpy(floatString
, intString2
, dpLocation
);
196 floatString
[dpLocation
] = '\0';
197 strcat(floatString
, decimalPoint
);
198 strcat(floatString
, intString2
+ dpLocation
);
203 // Simple and fast atof (ascii to float) function.
205 // - Executes about 5x faster than standard MSCRT library atof().
206 // - An attractive alternative if the number of calls is in the millions.
207 // - Assumes input is a proper integer, fraction, or scientific format.
208 // - Matches library atof() to 15 digits (except at extreme exponents).
209 // - Follows atof() precedent of essentially no error checking.
211 // 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
212 #define white_space(c) ((c) == ' ' || (c) == '\t')
213 #define valid_digit(c) ((c) >= '0' && (c) <= '9')
214 float fastA2F(const char *p
)
217 float sign
, value
, scale
;
219 // Skip leading white space, if any.
220 while (white_space(*p
)) {
230 } else if (*p
== '+') {
234 // Get digits before decimal point or exponent, if any.
236 while (valid_digit(*p
)) {
237 value
= value
* 10.0f
+ (*p
- '0');
241 // Get digits after decimal point, if any.
246 while (valid_digit(*p
)) {
247 value
+= (*p
- '0') / pow10
;
253 // Handle exponent, if any.
255 if ((*p
== 'e') || (*p
== 'E')) {
259 // Get sign of exponent, if any.
265 } else if (*p
== '+') {
269 // Get digits of exponent, if any.
271 while (valid_digit(*p
)) {
272 expon
= expon
* 10 + (*p
- '0');
278 // Calculate scaling factor.
279 // while (expon >= 50) { scale *= 1E50f; expon -= 50; }
290 // Return signed and scaled floating point result.
291 return sign
* (frac
? (value
/ scale
) : (value
* scale
));