I would like to know how to change the last x digits, of a series of double numbers, to zeros. E.g. I would like to change 1.0034958675848392 to 1.0034950000000000?
I have been searching C++.com for hours and I haven't come up with anything!
Use modf() to split the fractional part out, multiply it by the correct power of ten (six in your example), use floor(), divide by that same power of ten, and add the whole and fractional parts back together.
If you know your number is small enough that the multiplication won't loose digits, you can skip the modf() part.
It might not really be easily possible to cut your numbers to decimal points because they're stored in binary, but you might be able to do something close using bitwise operations, and bitshifting. I do this on integers all the time, but I can't say for certain if it would be very useful to floating point numbers.
Thanks for your suggestions. I've found a way around it. That is, outputting truncated values and then reading them back in! I don't think it matters that I'm not rounding the last digit because the precision is still very high. It's not an elegant solution, but it does the job.
xplore: Thanks for your suggestion, but setting the precision of the output wasn't the problem. But again, thanks for responding.
xabnu: Yes, I'd also thought of using bitwise operations and bitshifting. I'd read a bit about that a couple months ago. Now that I have a back up solution, I'm not feeling so anxious! So, I'll likely read, again, how to do that, and the Wikipedia entry that you suggested. Thank you for your response.
Also, I had to change this:
cout << fixed << setprecision(x = # of places past decimal point ) << data;
To this:
cout << setprecision(x = # of places past decimal point) << data;
-The solution that I found to my initial query involved me truncating the outputs. I simply printed out the contents of the matrices, without setting the precision. The output numbers, by default, had six significant figures. I read them back in and I had what I wanted!