|
| 1 | +// Copyright 2025 Matt Borland |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | +// |
| 5 | +// This file briefly demonstrates the difference in results when parsing monetary values between float and decimal32_t |
| 6 | + |
| 7 | +#include "where_file.hpp" |
| 8 | +#include <boost/decimal/decimal32_t.hpp> // For type decimal32_t |
| 9 | +#include <boost/decimal/charconv.hpp> // For support to <charconv> |
| 10 | +#include <boost/decimal/iostream.hpp> // For decimal support to <iostream> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
| 13 | +#include <fstream> |
| 14 | +#include <sstream> |
| 15 | +#include <limits> |
| 16 | +#include <numeric> |
| 17 | +#include <iostream> |
| 18 | +#include <iomanip> |
| 19 | + |
| 20 | +using boost::decimal::decimal32_t; |
| 21 | + |
| 22 | +template <typename T> |
| 23 | +T parse_opening_price(const std::string& line); |
| 24 | + |
| 25 | +template <> |
| 26 | +float parse_opening_price<float>(const std::string& line) |
| 27 | +{ |
| 28 | + const auto result {std::stof(line)}; |
| 29 | + return result; |
| 30 | +} |
| 31 | + |
| 32 | +template <> |
| 33 | +decimal32_t parse_opening_price<decimal32_t>(const std::string& line) |
| 34 | +{ |
| 35 | + decimal32_t result; |
| 36 | + const auto r = from_chars(line, result); |
| 37 | + |
| 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 |
| 42 | + if (!r) |
| 43 | + { |
| 44 | + // LCOV_EXCL_START |
| 45 | + result = std::numeric_limits<decimal32_t>::quiet_NaN(); |
| 46 | + BOOST_DECIMAL_THROW_EXCEPTION(std::invalid_argument("Parsing has failed")); |
| 47 | + // LCOV_EXCL_STOP |
| 48 | + } |
| 49 | + |
| 50 | + return result; |
| 51 | +} |
| 52 | + |
| 53 | +template <typename T> |
| 54 | +T parse_csv_line(const std::string& line) |
| 55 | +{ |
| 56 | + std::stringstream ss(line); |
| 57 | + std::string token; |
| 58 | + std::string date; |
| 59 | + |
| 60 | + std::getline(ss, date, ','); |
| 61 | + std::getline(ss, token, ','); |
| 62 | + |
| 63 | + return parse_opening_price<T>(token); |
| 64 | +} |
| 65 | + |
| 66 | +int main() |
| 67 | +{ |
| 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 | + |
| 72 | + // Open and read the CSV file |
| 73 | + std::ifstream file(boost::decimal::where_file("AAPL.csv")); |
| 74 | + std::string line; |
| 75 | + |
| 76 | + // Skip header line |
| 77 | + std::getline(file, line); |
| 78 | + |
| 79 | + std::vector<decimal32_t> decimal_opening_prices; |
| 80 | + std::vector<float> float_opening_prices; |
| 81 | + |
| 82 | + // Parse each line once into decimal32_t and once into a float |
| 83 | + while (std::getline(file, line)) |
| 84 | + { |
| 85 | + decimal_opening_prices.emplace_back(parse_csv_line<decimal32_t>(line)); |
| 86 | + float_opening_prices.emplace_back(parse_csv_line<float>(line)); |
| 87 | + } |
| 88 | + |
| 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"}; |
| 100 | + |
| 101 | + std::cout << std::setprecision(std::numeric_limits<float>::digits10 + 1) |
| 102 | + << "Number of data points: " << decimal_opening_prices.size() << '\n' |
| 103 | + << " Sum from MS Excel: " << ms_excel_result << '\n' |
| 104 | + << "Sum using decimal32_t: " << decimal_sum << '\n' |
| 105 | + << " Sum using float: " << float_sum << std::endl; |
| 106 | +} |
0 commit comments