Skip to content

Commit d457c94

Browse files
committed
Improve comments and code clarity
1 parent 197bd95 commit d457c94

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

examples/numerical_parsing.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ decimal32_t parse_opening_price<decimal32_t>(const std::string& line)
3535
decimal32_t result;
3636
const auto r = from_chars(line, result);
3737

38+
// If we have a parse failure throw std::invalid_argument if the environment supports it,
39+
// using std::invalid_argument which is the same thing thrown by std::stof in the float case
40+
//
41+
// If we are in a no throw environment returning a qNaN will poison our results as well
3842
if (!r)
3943
{
4044
// LCOV_EXCL_START
@@ -47,7 +51,7 @@ decimal32_t parse_opening_price<decimal32_t>(const std::string& line)
4751
}
4852

4953
template <typename T>
50-
auto parse_csv_line(const std::string& line) -> T
54+
T parse_csv_line(const std::string& line)
5155
{
5256
std::stringstream ss(line);
5357
std::string token;
@@ -61,6 +65,10 @@ auto parse_csv_line(const std::string& line) -> T
6165

6266
int main()
6367
{
68+
// We have a CSV file containing one years worth of daily stock data for AAPL
69+
// Here we will show the differences that arise (however small)
70+
// between parsing with float and decimal32_t
71+
6472
// Open and read the CSV file
6573
std::ifstream file(boost::decimal::where_file("AAPL.csv"));
6674
std::string line;
@@ -71,19 +79,28 @@ int main()
7179
std::vector<decimal32_t> decimal_opening_prices;
7280
std::vector<float> float_opening_prices;
7381

82+
// Parse each line once into decimal32_t and once into a float
7483
while (std::getline(file, line))
7584
{
7685
decimal_opening_prices.emplace_back(parse_csv_line<decimal32_t>(line));
7786
float_opening_prices.emplace_back(parse_csv_line<float>(line));
7887
}
7988

80-
const std::string ms_result {"52151.99"};
81-
const auto decimal_sum {std::accumulate(decimal_opening_prices.begin(), decimal_opening_prices.end(), decimal32_t{0})};
82-
const auto float_sum {std::accumulate(float_opening_prices.begin(), float_opening_prices.end(), float{0})};
89+
// Use std::accumulate to get the sum of all the pricing information in the array
90+
// This will be used to compare the total value parsed
91+
const auto decimal_sum {std::accumulate(decimal_opening_prices.begin(),
92+
decimal_opening_prices.end(), decimal32_t{0})};
93+
94+
const auto float_sum {std::accumulate(float_opening_prices.begin(),
95+
float_opening_prices.end(), float{0})};
96+
97+
// This is the reference value that was found using the sum command of the CSV
98+
// inside Microsoft Excel
99+
const std::string ms_excel_result {"52151.99"};
83100

84101
std::cout << std::setprecision(std::numeric_limits<float>::digits10 + 1)
85102
<< "Number of data points: " << decimal_opening_prices.size() << '\n'
86-
<< " Sum from MS Excel: " << ms_result << '\n'
103+
<< " Sum from MS Excel: " << ms_excel_result << '\n'
87104
<< "Sum using decimal32_t: " << decimal_sum << '\n'
88105
<< " Sum using float: " << float_sum << std::endl;
89106
}

0 commit comments

Comments
 (0)