Skip to content

Jira-575, Jira-506: Bug fixes to print float and double correctly, i… #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions cores/arduino/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,29 @@ size_t Print::printLongLong(unsigned long long n, uint8_t base) {
size_t Print::printFloat(double number, uint8_t digits)
{
size_t n = 0;
int exponent = 0;
double tmp;

if (isnan(number)) return print("nan");
if (isinf(number)) return print("inf");
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically

// Handle negative numbers
if (number < 0.0)
{
n += print('-');
number = -number;
if (number < 0.0) {
n += print('-');
number = -number;
}

// Chk if integer part has more than 8 digits.
tmp = number;
while (true) {
tmp /= 10.0;
exponent++;
if (tmp < 10.0) break;
}
if (exponent > 8)
number = tmp;
else
exponent = 0;

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
Expand Down Expand Up @@ -328,5 +339,11 @@ size_t Print::printFloat(double number, uint8_t digits)
remainder -= toPrint;
}

// Print the exponent portion
if (exponent) {
n += print("e+");
n += print(exponent);
}

return n;
}
8 changes: 4 additions & 4 deletions cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ String::String(float value, unsigned char decimalPlaces)
{
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
*this = dtostrf(value, (33 - 1), decimalPlaces, buf);
}

String::String(double value, unsigned char decimalPlaces)
{
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
*this = dtostrf(value, (33 - 1), decimalPlaces, buf);
}

String::~String()
Expand Down Expand Up @@ -363,14 +363,14 @@ unsigned char String::concat(unsigned long long num)
unsigned char String::concat(float num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
char* string = dtostrf(num, (20 - 1), 2, buf);
return concat(string, strlen(string));
}

unsigned char String::concat(double num)
{
char buf[20];
char* string = dtostrf(num, 4, 2, buf);
char* string = dtostrf(num, (20 - 1), 2, buf);
return concat(string, strlen(string));
}

Expand Down
79 changes: 73 additions & 6 deletions cores/arduino/stdlib_noniso.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,76 @@ char* ultoa( unsigned long val, char *string, int radix )
return string;
}

char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}
char * dtostrf(double number, unsigned char width, unsigned char prec, char *s) {

if (isnan(number)) {
strcpy(s, "nan");
return s;
}
if (isinf(number)) {
strcpy(s, "inf");
return s;
}

char* out = s;
int exponent = 0;
unsigned char len, expLen;
double tmp;

// Handle negative numbers
if (number < 0.0) {
*out++ = '-';
number = -number;
}

// The integer portion has to be <= 8 digits. Otherwise, the
// string is in exponent format.
tmp = number;
for (;;) {
tmp /= 10.0;
exponent++;
if (tmp < 10.0) break;
}
if (exponent > 8)
number = tmp;
else
exponent = 0;

// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < prec; ++i)
rounding /= 10.0;

number += rounding;

// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
out += sprintf(out, "%ld", int_part);

// Don't go beyond the given width of the string
len = (unsigned char)(out - s);
expLen = (exponent == 0) ? 0 : 5; // 5 places for exponent expression
if ((prec + len + expLen) > width)
prec = width - len - expLen;

// Print the decimal point, but only if there are digits beyond
if (prec > 0) {
*out = '.';
++out;
prec--;
// Copy character by character to 'out' string
for (unsigned char decShift = prec; decShift > 0; decShift--) {
remainder *= 10.0;
sprintf(out, "%d", (int)remainder);
out++;
remainder -= (double)(int)remainder;
}
}

// Print the exponent if exists
if (exponent)
sprintf(out, "e+%.3d", exponent);

return s;
}
4 changes: 2 additions & 2 deletions cores/arduino/stdlib_noniso.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ char* utoa (unsigned int val, char *s, int radix);

char* ultoa (unsigned long val, char *s, int radix);

char* dtostrf (double val, signed char width, unsigned char prec, char *s);
char* dtostrf (double val, unsigned char width, unsigned char prec, char *s);

#ifdef __cplusplus
} // extern "C"
#endif

#endif
#endif