Skip to content

Commit

Permalink
Overflow errors with attempted fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlaucsb committed Mar 25, 2019
1 parent c487812 commit abaae6e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ namespace csv::internals {
bool prob_float = false;

unsigned places_after_decimal = 0;
unsigned long significand = 0;
long double integral_part = 0,
decimal_part = 0;

for (size_t i = 0, ilen = in.size(); i < ilen; i++) {
const char& current = in[i];
Expand Down Expand Up @@ -95,9 +96,11 @@ namespace csv::internals {
unsigned digit = current - '0';
if (prob_float) {
places_after_decimal++;
decimal_part = (decimal_part * 10) + digit;
}
else {
integral_part = (integral_part * 10) + digit;
}

significand = (significand * 10) + digit;
}
else {
return CSV_STRING;
Expand All @@ -107,7 +110,7 @@ namespace csv::internals {

// No non-numeric/non-whitespace characters found
if (has_digit) {
long double number = significand * pow(10, -(double)places_after_decimal);
long double number = integral_part + decimal_part * pow(10, -(double)places_after_decimal);
if (out) *out = neg_allowed ? number : -number;

if (prob_float)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_read_csv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ TEST_CASE("Test read_row() CSVField - Memory", "[read_row_csvf2]") {
// Fourth Row
rows.pop_front();
row = rows.front();
double big_num_csv = row[0].get<double>();
REQUIRE(row[0].type() == CSV_DOUBLE); // Overflow
REQUIRE(internals::is_equal(row[0].get<double>(), big_num));
REQUIRE(internals::is_equal(big_num_csv, big_num));
}

TEST_CASE("Test read_row() CSVField - Power Status", "[read_row_csvf3]") {
Expand Down

0 comments on commit abaae6e

Please sign in to comment.