-
-
Notifications
You must be signed in to change notification settings - Fork 129
Test Stream::parseFloat() with many input digits #133
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
Changes from 4 commits
129ae52
8eb4013
5445db3
fae13e5
1266b08
91c5e29
6197511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
|
||
#include "Common.h" | ||
#include "Stream.h" | ||
#include <math.h> | ||
|
||
#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait | ||
|
||
|
@@ -162,9 +163,9 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) | |
{ | ||
bool isNegative = false; | ||
bool isFraction = false; | ||
long value = 0; | ||
double value = 0.0; | ||
int c; | ||
float fraction = 1.0; | ||
unsigned int digits_post_comma = 0; | ||
|
||
c = peekNextDigit(lookahead, true); | ||
// ignore non numeric leading characters | ||
|
@@ -181,7 +182,7 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) | |
else if(c >= '0' && c <= '9') { // is c a digit? | ||
value = value * 10 + c - '0'; | ||
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.
if(isFraction) {
fraction *= 0.1;
value = value + fraction * (c - '0');
} else {
value = value * 10 + c - '0';
} Note that on something other than AVR one would need more than 308 digits to demonstrate the problem. 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. I like this form, however in that case we've got to commit to using a I can further support my argument pro floating point implementation that if you want to run performance-critical/floating-point applications on an 8-Bit architecture you've selected the wrong MCU alltogether. 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. Since I got no feedback over quite a long period of time (unfortunate but not unexpected given what we are currently swamped in) I'm moving forward with merging this PR. 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. Let's hold on for a second though @edgar-bonet can you integrate your last change suggested above by yourself: if(isFraction) {
fraction *= 0.1;
value = value + fraction * (c - '0');
} else {
value = value * 10 + c - '0';
} 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. @aentinger: OK, I'll do it today. |
||
if(isFraction) | ||
fraction *= 0.1; | ||
digits_post_comma++; | ||
} | ||
read(); // consume the character we got with peek | ||
c = timedPeek(); | ||
|
@@ -190,10 +191,11 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) | |
|
||
if(isNegative) | ||
value = -value; | ||
|
||
if(isFraction) | ||
return value * fraction; | ||
else | ||
return value; | ||
value /= pow(10, digits_post_comma); | ||
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.
|
||
|
||
return value; | ||
} | ||
|
||
// read characters from stream into buffer | ||
|
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.
If
value
is a floating point number, there is no need for this extra variable. It could be folded intovalue
.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.
That's a good question here. What's more computing time expensive -
pow
or repeatedly doing a double multiplication?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.
However, a further argument against
pow
is that there's a question of availability (and with what argument types) across all supported platforms.