0

I know we can use setprecision to prevent a number to be rounded but what if I don't want to round all of my numbers in the list with the same decimal places? In other words, I want to keep my numbers the same as calculated and each number has its own decimal places.

For example: if we use setprecision(7) then it will give the output up to 7 decimal places for all numbers in my list. What if I have a list of different numbers with different decimal places such as 8 or 10 decimal places? Do I need to do setprecision for each of them? Is there any way to keep my numbers the same as the output after calculated? Ex:0.1234567, 0.123456789, 0.12345678910

#include<iostream>
#include<vector>
using namespace std;
int main()
{
   vector<double> myVec{0.1234567, 0.123456789, 0.12345678, 0.12345678910};

   for(int i = 0; i < myVec.size(); i++)
   {
       cout << myVec[i] << " ";
   }
   return 0;
}
Si Dang
  • 25
  • 1
  • 6
  • 1
    It would help if you could provide a small code sample which illustrated your point. 10 lines of code = 100 words, after all. – mackycheese21 Apr 21 '20 at 14:33
  • What do you mean by "keep my numbers the same as calculated"? Decimal places are not part of the number stored in memory. – stark Apr 21 '20 at 14:33
  • 1
    `setprecision` controls output but has no effect on the number, hence your quesiton is rather unclear – 463035818_is_not_an_ai Apr 21 '20 at 14:34
  • @mackycheese21 I added the code, I want to keep my output the same as the myVec. – Si Dang Apr 21 '20 at 14:38
  • You know that the values you set are not really the ones stored in memory? If not, check out [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Keldorn Apr 21 '20 at 14:40
  • 1
    It sounds like you want to use a vector of string representations of numbers rather than a vector of doubles. – L. Scott Johnson Apr 21 '20 at 14:43
  • @Keldorn Thank you for giving me that link, really helpful. I also want the output the same as the myVec but it keeps rounding. I do not want to setprecision for each element because each element has its own decimal places length. I hope you see what I mean :( – Si Dang Apr 21 '20 at 14:44

1 Answers1

0

Is there any way to keep my numbers the same as the output after calculated?

Not with floating point types such as double.

These are binary floating points, not decimal. Many exact finite decimal fractions will convert to infinite binary fraction, and rounding would happen. So after you do dobule d = 0.1234567, the variable d will not contain exactly 0.1234567, it is rather something close to 0.1234567 representable as double.

A way to deal with this is to have custom data types that are decimal fractions, with fixed or floating point, so that 0.1234567 could be represented exactly.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79
  • Yes, that is my problem. I hope I can know a specific "super data type" for that but I don't think C++ has. :(( – Si Dang Apr 21 '20 at 14:46
  • Standard C++ does not, but there are libraries. Look at boost multiprecision. `cpp_dec_float_50` may be what you want. – Alex Guteniev Apr 21 '20 at 14:49