0

I am attempting to output long doubles to a text file, But when outputting the data I am getting data such as.

0.1,0.1,0.1

When I am expecting.

0.10000000000480658, 0.10000000632434904, 0.10000000000000001

The code I am currently using is below.

        ofstream fileDump("2Stars.dump.csv");

        for (auto star: star_list) {
            int cycle_id = 1;
            for (auto history : star->history_position) {
                fileDump << star->id << ',' << cycle_id << ',' << history.x << ',' << history.y << ',' << history.z << std::endl;
                ++cycle_id;
            }
        }
        fileDump.close();

I have seen a few 'solutions' to similar issues but all of them seem to involve using string streams that don't seem to work any differently. I have also tried using toString but that just outputted 0.1000.

  • `static auto to_long_string(double d) -> string { ostringstream ss; ss << fixed << setprecision(80) << d; auto s = ss.str(); while (!s.empty() && s.back() == '0') s.pop_back(); if (!s.empty() && s.back() == '.') s.pop_back(); return s; }` – Eljay Oct 28 '22 at 13:01

0 Answers0