Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2af4a9c

Browse filesBrowse files
authored
Merge pull request #133 from edgar-bonet/many-digits
Test Stream::parseFloat() with many input digits
2 parents fdccb19 + 6197511 commit 2af4a9c
Copy full SHA for 2af4a9c

File tree

2 files changed

+22
-9
lines changed
Filter options

2 files changed

+22
-9
lines changed

‎api/Stream.cpp

Copy file name to clipboardExpand all lines: api/Stream.cpp
+10-9Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
162162
{
163163
bool isNegative = false;
164164
bool isFraction = false;
165-
long value = 0;
165+
double value = 0.0;
166166
int c;
167-
float fraction = 1.0;
167+
double fraction = 1.0;
168168

169169
c = peekNextDigit(lookahead, true);
170170
// ignore non numeric leading characters
@@ -179,9 +179,12 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
179179
else if (c == '.')
180180
isFraction = true;
181181
else if(c >= '0' && c <= '9') { // is c a digit?
182-
value = value * 10 + c - '0';
183-
if(isFraction)
184-
fraction *= 0.1;
182+
if(isFraction) {
183+
fraction *= 0.1;
184+
value = value + fraction * (c - '0');
185+
} else {
186+
value = value * 10 + c - '0';
187+
}
185188
}
186189
read(); // consume the character we got with peek
187190
c = timedPeek();
@@ -190,10 +193,8 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
190193

191194
if(isNegative)
192195
value = -value;
193-
if(isFraction)
194-
return value * fraction;
195-
else
196-
return value;
196+
197+
return value;
197198
}
198199

199200
// read characters from stream into buffer

‎test/src/Stream/test_parseFloat.cpp

Copy file name to clipboardExpand all lines: test/src/Stream/test_parseFloat.cpp
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <StreamMock.h>
1212

13+
#include <float.h>
14+
1315
/**************************************************************************************
1416
* TEST CODE
1517
**************************************************************************************/
@@ -43,6 +45,16 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore =
4345
mock << "\r\n\t 12.34";
4446
REQUIRE(mock.parseFloat() == 12.34f);
4547
}
48+
WHEN ("A float is provided with too many digits after the decimal point")
49+
{
50+
mock << "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870064";
51+
REQUIRE(mock.parseFloat() == Approx(3.141592654f));
52+
}
53+
WHEN ("A float is larger than LONG_MAX")
54+
{
55+
mock << "602200000000000000000000.00";
56+
REQUIRE(mock.parseFloat() == Approx(6.022e23f));
57+
}
4658
}
4759

4860
TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.