Skip to content
Open
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
6 changes: 4 additions & 2 deletions hardware/arduino/avr/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <float.h>

#include "WString.h"

/*********************************************/
Expand Down Expand Up @@ -108,14 +110,14 @@ String::String(unsigned long value, unsigned char base)
String::String(float value, unsigned char decimalPlaces)
{
init();
char buf[33];
char buf[FLT_MAX_10_EXP + 4 + decimalPlaces]; // +4, one for: 10, -, ., \0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with "10" there? I can understand how -, . and \0 are added to the number of digits that FLT_MAX_10_EXP represents, but not what 10 means.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 as in ascii 10, being a LineFeed perhaps ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On AVR DBL_MAX_10_EXP is 38 and Serial.println(String(-DBL_MAX)); prints out -340282350000000000000000000000000000000.00.

Which is 43 characters in length, then you need an extra byte for the \0, which brings us to 44.

44 - 38 - 2 = 4. Maybe, I was thinking the extra character was for the 1's digit ...

*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

String::String(double value, unsigned char decimalPlaces)
{
init();
char buf[33];
char buf[DBL_MAX_10_EXP + 4 + decimalPlaces]; // +4, one for: 10, -, ., \0
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand Down
6 changes: 4 additions & 2 deletions hardware/arduino/sam/cores/arduino/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <float.h>

#include "WString.h"
#include "itoa.h"
#include "avr/dtostrf.h"
Expand Down Expand Up @@ -110,14 +112,14 @@ String::String(unsigned long value, unsigned char base)
String::String(float value, unsigned char decimalPlaces)
{
init();
char buf[33];
char buf[FLT_MAX_10_EXP + 4 + decimalPlaces]; // +4, one for: 10, -, ., \0
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

String::String(double value, unsigned char decimalPlaces)
{
init();
char buf[33];
char buf[DBL_MAX_10_EXP + 4 + decimalPlaces]; // +4, one for: 10, -, ., \0
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand Down