File tree 2 files changed +22
-9
lines changed
Filter options
2 files changed +22
-9
lines changed
Original file line number Diff line number Diff line change @@ -162,9 +162,9 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
162
162
{
163
163
bool isNegative = false ;
164
164
bool isFraction = false ;
165
- long value = 0 ;
165
+ double value = 0. 0 ;
166
166
int c;
167
- float fraction = 1.0 ;
167
+ double fraction = 1.0 ;
168
168
169
169
c = peekNextDigit (lookahead, true );
170
170
// ignore non numeric leading characters
@@ -179,9 +179,12 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
179
179
else if (c == ' .' )
180
180
isFraction = true ;
181
181
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
+ }
185
188
}
186
189
read (); // consume the character we got with peek
187
190
c = timedPeek ();
@@ -190,10 +193,8 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore)
190
193
191
194
if (isNegative)
192
195
value = -value;
193
- if (isFraction)
194
- return value * fraction;
195
- else
196
- return value;
196
+
197
+ return value;
197
198
}
198
199
199
200
// read characters from stream into buffer
Original file line number Diff line number Diff line change 10
10
11
11
#include < StreamMock.h>
12
12
13
+ #include < float.h>
14
+
13
15
/* *************************************************************************************
14
16
* TEST CODE
15
17
**************************************************************************************/
@@ -43,6 +45,16 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore =
43
45
mock << " \r\n\t 12.34" ;
44
46
REQUIRE (mock.parseFloat () == 12 .34f );
45
47
}
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
+ }
46
58
}
47
59
48
60
TEST_CASE (" Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)" , " [Stream-parseFloat-02]" )
You can’t perform that action at this time.
0 commit comments