0

I've got a .txt file with multiple x y and z float numbers and I'm getting line by line with std::getline(file, line).

My problem is: while I'm getting the values correctly for x, y and z in strings, their decimal places are being reduced and that's not what I want.

I want to know how to store the full value. From what I saw I can use std::setprecision() to correct this while printing, but I want to correct the stored values so I can use them.

What can I do? Are the numbers stored properly but not shown properly by my std::cout? Here's the code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <regex>


int main()
{
    std::string line;
    std::ifstream input("input.txt");
    std::vector<float> x;
    std::vector<float> y;
    std::vector<float> z;
    std::regex reg("[,]+");
    int line_count = 0;
    if (input.is_open()) {
        while (std::getline(input, line)) {
            if (line_count > 0)
            {
                std::sregex_token_iterator iter(line.begin(), line.end(), reg, -1);
                std::sregex_token_iterator end;

                std::vector<std::string> tokens(iter, end);

                std::cout << tokens[0] << std::endl;

                x.push_back(std::stof(tokens[0]));
                std::cout << x[line_count - 1] << std::endl;
                y.push_back(std::stof(tokens[1]));
                z.push_back(std::stof(tokens[2]));
            }
            line_count++;
        }
    }
    /*for (size_t i = 0; i < x.size(); i++)
    {
        std::cout << x[i] << " " << y[i] << " " << z[i] << std::endl;
    }*/
}

The .txt file is as follows:

x,y,z
-0.015869140625,0.896728515625,-0.103515625
-0.00634765625,0.8935546875,-0.147216796875
-0.00634765625,0.8935546875,-0.147216796875
-0.02197265625,0.9326171875,-0.10400390625
-0.02197265625,0.9326171875,-0.10400390625
-0.078369140625,0.944580078125,-0.126220703125
-0.078369140625,0.944580078125,-0.126220703125
-0.047119140625,0.979248046875,-0.114990234375
-0.047119140625,0.979248046875,-0.114990234375
0.022216796875,1.0068359375,-0.096435546875
-0.009033203125,1.02685546875,-0.078369140625
-0.009033203125,1.02685546875,-0.078369140625
-0.052490234375,1.033935546875,-0.114501953125
  • 1
    How about using `double` instead of `float` for better precision? – K.R.Park Feb 17 '22 at 04:25
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Passerby Feb 17 '22 at 04:26
  • You can use cout.precision(Possible_Max_Digit). For example: cout.precision(15); std::cout << x[i] << " " << y[i] << " " << z[i] << std::endl; – Md. Faisal Habib Feb 17 '22 at 04:53
  • A `float` typically does not have the precision to represent that many decimals. If you can't accept getting approximations of the values you're parsing, you should use `double` instead and, therefore, `std::stod`. – Etienne de Martel Feb 17 '22 at 04:58
  • i've tried using stod and double vectors but it ended up the same. Anything else I can do? – Vinícius Inacio Breda Feb 17 '22 at 11:07

1 Answers1

0

Double and float do not have that precision. You can use The GNU Multiple Precision Arithmetic Library class mpf_class.

ouflak
  • 2,458
  • 10
  • 44
  • 49