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/>.
27 #include "build/build_config.h"
30 #ifdef REQUIRE_PRINTF_LONG_SUPPORT
32 void uli2a(unsigned long int num
, unsigned int base
, int uc
, char *bf
)
36 while (num
/ d
>= base
)
41 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
50 void li2a(long num
, char *bf
)
56 uli2a(num
, 10, 0, bf
);
61 void ui2a(unsigned int num
, unsigned int base
, int uc
, char *bf
)
65 while (num
/ d
>= base
)
70 *bf
++ = dgt
+ (dgt
< 10 ? '0' : (uc
? 'A' : 'a') - 10);
79 void i2a(int num
, char *bf
)
90 if (ch
>= '0' && ch
<= '9')
92 else if (ch
>= 'a' && ch
<= 'f')
94 else if (ch
>= 'A' && ch
<= 'F')
100 char a2i(char ch
, const char **src
, int base
, int *nump
)
102 const char *p
= *src
;
105 while ((digit
= a2d(ch
)) >= 0) {
108 num
= num
* base
+ digit
;
116 #ifndef HAVE_ITOA_FUNCTION
119 ** The following two functions together make up an itoa()
120 ** implementation. Function i2a() is a 'private' function
121 ** called by the public itoa() function.
123 ** itoa() takes three arguments:
124 ** 1) the integer to be converted,
125 ** 2) a pointer to a character conversion buffer,
126 ** 3) the radix for the conversion
127 ** which can range between 2 and 36 inclusive
128 ** range errors on the radix default it to base10
129 ** Code from http://groups.google.com/group/comp.lang.c/msg/66552ef8b04fe1ab?pli=1
132 static char *_i2a(unsigned i
, char *a
, unsigned base
)
135 a
= _i2a(i
/ base
, a
, base
);
136 *a
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i
% base
];
140 char *itoa(int i
, char *a
, int base
)
142 if ((base
< 2) || (base
> 36))
146 *_i2a(-(unsigned) i
, a
+ 1, base
) = 0;
148 *_i2a(i
, a
, base
) = 0;
154 char *ftoa(float x
, char *floatString
)
158 char intString2
[12] = { 0, };
159 char *decimalPoint
= ".";
162 if (x
> 0) // Rounding for x.xxx display format
167 value
= (int32_t)(x
* 1000.0f
); // Convert float * 1000 to an integer
169 itoa(ABS(value
), intString1
, 10); // Create string from abs of integer value
172 intString2
[0] = ' '; // Positive number, add a pad space
174 intString2
[0] = '-'; // Negative number, add a negative sign
176 if (strlen(intString1
) == 1) {
180 strcat(intString2
, intString1
);
181 } else if (strlen(intString1
) == 2) {
184 strcat(intString2
, intString1
);
185 } else if (strlen(intString1
) == 3) {
187 strcat(intString2
, intString1
);
189 strcat(intString2
, intString1
);
192 dpLocation
= strlen(intString2
) - 3;
194 memcpy(floatString
, intString2
, dpLocation
);
195 floatString
[dpLocation
] = '\0';
196 strcat(floatString
, decimalPoint
);
197 strcat(floatString
, intString2
+ dpLocation
);
202 // Simple and fast atof (ascii to float) function.
204 // - Executes about 5x faster than standard MSCRT library atof().
205 // - An attractive alternative if the number of calls is in the millions.
206 // - Assumes input is a proper integer, fraction, or scientific format.
207 // - Matches library atof() to 15 digits (except at extreme exponents).
208 // - Follows atof() precedent of essentially no error checking.
210 // 09-May-2009 Tom Van Baak (tvb) www.LeapSecond.com
211 #define white_space(c) ((c) == ' ' || (c) == '\t')
212 #define valid_digit(c) ((c) >= '0' && (c) <= '9')
213 float fastA2F(const char *p
)
216 float sign
, value
, scale
;
218 // Skip leading white space, if any.
219 while (white_space(*p
)) {
229 } else if (*p
== '+') {
233 // Get digits before decimal point or exponent, if any.
235 while (valid_digit(*p
)) {
236 value
= value
* 10.0f
+ (*p
- '0');
240 // Get digits after decimal point, if any.
245 while (valid_digit(*p
)) {
246 value
+= (*p
- '0') / pow10
;
252 // Handle exponent, if any.
254 if ((*p
== 'e') || (*p
== 'E')) {
258 // Get sign of exponent, if any.
264 } else if (*p
== '+') {
268 // Get digits of exponent, if any.
270 while (valid_digit(*p
)) {
271 expon
= expon
* 10 + (*p
- '0');
277 // Calculate scaling factor.
278 // while (expon >= 50) { scale *= 1E50f; expon -= 50; }
289 // Return signed and scaled floating point result.
290 return sign
* (frac
? (value
/ scale
) : (value
* scale
));