-
-
Notifications
You must be signed in to change notification settings - Fork 140
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'; | ||
|
||
| 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); | ||
|
||
|
|
||
| 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
valueis 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 -
powor 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
powis that there's a question of availability (and with what argument types) across all supported platforms.