1
int main(int argc, char *argv[])
{
    std::deque<float> queue;
    queue.push_back(1.23456789);
    queue.push_back(2.3456789);
    queue.push_back(3.456789);
    queue.push_back(4.56789);
    queue.push_back(5.6789);

    std::ofstream file ("out.txt");
    if (file.is_open())
    {        
        file.precision(12);        
        std::copy(queue.begin(), queue.end(), std::ostream_iterator<float>(file, ","));
    }
    file.close();

    return 0;
}

I'm running the code above and when I open out.txt I got the following written:

1.23456788063;2.3456788063;3.45678901672;4.56789016724;5.67889976501;

I was expecting:

1.23456789;2.3456789;3.456789;4.56789;5.6789;

or at least:

1.23456789000;2.3456789000;3.45678900000;4.56789000000;5.67890000000;

What am I doing wrong here?

[EDIT]

Ok, it is in the nature of floating points, I accept it and I can live with it. But still, if I want to get my expected outcome, what should I do? What is the best workaround?

[EDIT]

guneykayim
  • 5,210
  • 2
  • 29
  • 61

0 Answers0