Skip to content
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

Increase buffer size for converting floats + doubles to String's #5369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sandeepmistry
Copy link
Contributor

Port of arduino/ArduinoCore-samd#163

Troublesome sketch:

#include <float.h> 

void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println(String(FLT_MAX));
  Serial.println(String(DBL_MAX));
  Serial.println(String(-FLT_MAX));
  Serial.println(String(-DBL_MAX));
}

void loop() {
}

@@ -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 ...

@facchinm
Copy link
Member

facchinm commented Feb 9, 2017

Partially moved to arduino/ArduinoCore-sam#32

@ArduinoBot
Copy link
Contributor

❌ Build failed.

@facchinm facchinm added the Print and Stream class The Arduino core library's Print and Stream classes label Feb 28, 2017
@facchinm facchinm removed this from the Release 1.8.2 milestone Feb 28, 2017
@facchinm facchinm added this to the Release 1.8.3 milestone Mar 20, 2017
@cmaglie cmaglie modified the milestones: Release 1.8.3, Next Jul 18, 2017
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Print and Stream class The Arduino core library's Print and Stream classes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants