/****************************************************************************** * This program demonstrates floating point error. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ #include #include #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); double oneThird = 1. / 3.; wcout << fmt::format(L"1/3: {0:.16f}", oneThird) << endl; wcout << fmt::format(L"one: {0:.16f}", oneThird * 3.0) << endl; wcout << fmt::format(L"three: {0:.16f}", oneThird * 9.0) << endl; wcout << fmt::format(L"hundred: {0:.16f}", oneThird * 300.0) << endl; double twoTenths = 0.2; double sum = 0.0; for (int x = 0; x < 1000; ++x) { sum += twoTenths; } wcout << fmt::format(L"twoTenths: {0:.16f}", twoTenths) << endl; wcout << fmt::format(L"sum: {0:.16f}", sum) << endl; wcout << fmt::format(L"200: {0:.16f}", twoTenths * 1000.) << endl; return 0; }