-
Notifications
You must be signed in to change notification settings - Fork 13.3k
restore dtostrf when floats are disabled in printf/scanf + round fix #7087
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c07a1fd
Corrected stack start and end in stack_thunk_dump_stack().
mhightower83 04b3e13
restore dtostrf+fix when float printing is disabled at link time
d-a-v 799795d
fix include file
d-a-v 321d256
Merge pull request #7085 from mhightower83/pr-bear-stack-dump
earlephilhower bc4f000
package builder: updates for alpha releases (#7088)
d-a-v e752e96
lwip2 updates: no more git sub-sub-module deps, faster checksum, back…
d-a-v f066ed2
configTime(tzsec,dstsec,): fix UTC/local management (#6993)
d-a-v ca971ea
Merge branch 'dtostrfix' of github.com:d-a-v/Arduino into dtostrfix
d-a-v c646c7e
fix with proposal per review
d-a-v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <math.h> | ||
#include <limits> | ||
#include "stdlib_noniso.h" | ||
|
||
extern "C" { | ||
|
@@ -40,11 +41,94 @@ char* ultoa(unsigned long value, char* result, int base) { | |
return utoa((unsigned int)value, result, base); | ||
} | ||
|
||
#ifdef NOPRINTFLOAT | ||
|
||
char * dtostrf(double number, signed char width, unsigned char prec, char *s) { | ||
bool negative = false; | ||
|
||
if (isnan(number)) { | ||
strcpy(s, "nan"); | ||
return s; | ||
} | ||
if (isinf(number)) { | ||
strcpy(s, "inf"); | ||
return 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 | ||
if (number < 0.0) { | ||
negative = true; | ||
fillme--; | ||
number = -number; | ||
} | ||
|
||
// Round correctly so that print(1.999, 2) prints as "2.00" | ||
// I optimized out most of the divisions | ||
double rounding = 2.0; | ||
for (uint8_t i = 0; i < prec; ++i) | ||
rounding *= 10.0; | ||
rounding = 1.0 / rounding; | ||
|
||
number += rounding; | ||
|
||
// Figure out how big our number really is | ||
double tenpow = 1.0; | ||
int digitcount = 1; | ||
double nextpow; | ||
while (number >= (nextpow = (10.0 * tenpow))) { | ||
tenpow = nextpow; | ||
digitcount++; | ||
} | ||
|
||
// minimal compensation for possible lack of precision | ||
number *= 1 + std::numeric_limits<decltype(number)>::epsilon(); | ||
|
||
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; | ||
while (digitcount-- > 0) { | ||
digit = (int8_t)number; | ||
if (digit > 9) digit = 9; // insurance | ||
*out++ = (char)('0' | digit); | ||
if ((digitcount == prec) && (prec > 0)) { | ||
*out++ = '.'; | ||
} | ||
number -= digit; | ||
number *= 10.0; | ||
} | ||
|
||
// make sure the string is terminated | ||
*out = 0; | ||
return s; | ||
} | ||
|
||
#else // !NOPRINTFLOAT | ||
|
||
char * dtostrf(double number, signed char width, unsigned char prec, char *s) { | ||
char fmt[32]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I mentioned in the issue, does this need to have a safeguard such as
|
||
sprintf(fmt, "%%%d.%df", width, prec); | ||
sprintf(s, fmt, number); | ||
return s; | ||
} | ||
|
||
#endif // !NOPRINTFLOAT | ||
|
||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NO_PRINTF_FLOAT
?NO_FLOAT_PRINTF
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dogpiling on this (sorry). Adding new defines normally ends up breaking PIO or other folks.
Would it be possible, through whatever magic the
-u _printf_float
uses, to somehow make the proper version be linked-in?I could also drag this into the
newlib
if that would be needed to make it work "seamlessly" with the existing infra/flags.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of it as something like the existing NO_GLOBAL_... defines
PIO user btw, only change that would be required is adding
env.Append(CXXFLAGS=["NO_PRINTF_FLOAT"])
to the user build script (or, modifying platformio.ini build_flags=... variable, which is also file that is controlled by user), Core files don't need to changeRe 'magic', it's just a simple
!= NULL
:https://github.com/earlephilhower/newlib-xtensa/blob/b326aa447a1e5b33d6ed7d164aa2c59eb3754b9f/newlib/libc/stdio/nano-vfprintf.c#L651-L653
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, then could it be as simple as:
This way the existing flag which will be eval'd a compile time will either cause the dtostrf guts to be omitted and use printf, or vice-versa.
No flag changes, no tool flow changes. Assuming it works...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great ! 👍 thanks @mcspr @earlephilhower