1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-06 05:21:22 +03:00

integrate new version provided by Odometer

This commit is contained in:
Martin Ayotte 2015-08-08 15:25:08 -04:00
parent 659e467141
commit 1cd99391c3

View File

@ -148,6 +148,7 @@ char* ultoa(unsigned long value, char* result, int base) {
} }
char * dtostrf(double number, signed char width, unsigned char prec, char *s) { char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
bool negative = false;
if (isnan(number)) { if (isnan(number)) {
strcpy(s, "nan"); strcpy(s, "nan");
@ -158,14 +159,17 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
return s; return s;
} }
if (number > 4294967040.0 || number < -4294967040.0) {
strcpy(s, "ovf");
return s;
}
char* out = s; char* out = s;
int fillme = width; // how many cells to fill for the integer part
if (prec > 0) {
fillme -= (prec+1);
}
// Handle negative numbers // Handle negative numbers
if (number < 0.0) { if (number < 0.0) {
*out++ = '-'; negative = true;
fillme--;
number = -number; number = -number;
} }
@ -178,34 +182,42 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
number += rounding; number += rounding;
// Extract the integer part of the number and print it // Figure out how big our number really is
unsigned long int_part = (unsigned long)number; double tenpow = 1.0;
double remainder = number - (double)int_part; int digitcount = 1;
out += sprintf(out, "%d", int_part); while (number >= 10.0 * tenpow) {
tenpow *= 10.0;
// Print the decimal point, but only if there are digits beyond digitcount++;
if (prec > 0) {
*out++ = '.';
} }
// make sure the string is terminated before mesuring it length
*out = 0;
// Reduce minimum width accordingly
width -= strlen(s);
// Print the digits after the decimal point number /= tenpow;
fillme -= digitcount;
// Pad unused cells with spaces
while (fillme-- > 0) {
*out++ = ' ';
}
// Handle negative sign
if (negative) *out++ = '-';
// Print the digits, and if necessary, the decimal point
digitcount += prec;
int8_t digit = 0; int8_t digit = 0;
while (prec-- > 0) { while (digitcount-- > 0) {
remainder *= 10.0; digit = (int8_t)number;
digit = (int8_t)remainder;
if (digit > 9) digit = 9; // insurance if (digit > 9) digit = 9; // insurance
*out++ = (char)('0' | digit); *out++ = (char)('0' | digit);
width--; if ((digitcount == prec) && (prec > 0)) {
remainder -= digit; *out++ = '.';
}
number -= digit;
number *= 10.0;
} }
// add '0' to fill minimum width requirement
while (width-- > 0) *out++ = ' ';
// make sure the string is terminated // make sure the string is terminated
*out = 0; *out = 0;
return s; return s;
} }