diff --git a/api/Stream.cpp b/api/Stream.cpp index a5d8b076..f6f9bda6 100644 --- a/api/Stream.cpp +++ b/api/Stream.cpp @@ -162,9 +162,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; + double fraction = 1.0; c = peekNextDigit(lookahead, true); // ignore non numeric leading characters @@ -179,9 +179,12 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) else if (c == '.') isFraction = true; else if(c >= '0' && c <= '9') { // is c a digit? - value = value * 10 + c - '0'; - if(isFraction) - fraction *= 0.1; + if(isFraction) { + fraction *= 0.1; + value = value + fraction * (c - '0'); + } else { + value = value * 10 + c - '0'; + } } read(); // consume the character we got with peek c = timedPeek(); @@ -190,10 +193,8 @@ float Stream::parseFloat(LookaheadMode lookahead, char ignore) if(isNegative) value = -value; - if(isFraction) - return value * fraction; - else - return value; + + return value; } // read characters from stream into buffer diff --git a/test/src/Stream/test_parseFloat.cpp b/test/src/Stream/test_parseFloat.cpp index 610f96a0..19d2ce86 100644 --- a/test/src/Stream/test_parseFloat.cpp +++ b/test/src/Stream/test_parseFloat.cpp @@ -10,6 +10,8 @@ #include +#include + /************************************************************************************** * TEST CODE **************************************************************************************/ @@ -43,6 +45,16 @@ TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = mock << "\r\n\t 12.34"; REQUIRE(mock.parseFloat() == 12.34f); } + WHEN ("A float is provided with too many digits after the decimal point") + { + mock << "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870064"; + REQUIRE(mock.parseFloat() == Approx(3.141592654f)); + } + WHEN ("A float is larger than LONG_MAX") + { + mock << "602200000000000000000000.00"; + REQUIRE(mock.parseFloat() == Approx(6.022e23f)); + } } TEST_CASE ("Testing parseFloat(LookaheadMode lookahead = SKIP_NONE, char ignore = NO_IGNORE_CHAR)", "[Stream-parseFloat-02]") pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy