-3

Hey so I'm making a function that returns the number of decimals, whole numbers, or TOTAL numbers there are, but have been unable to make it work with either of these ways:

  • multiplying by a really large number like 10 billion doesn't work because of the innacurate way computers store decimals, making 2.3 2.2999575697

  • Using a StringStream to convert the number to a string and count the characters doesn't work because it requires you to set the stream to a precision which either takes away or adds unnecesary '0' characters if not set to the actual number of decimals

So WHAT DO I DO NOW? somebody please help =( Thanks!

if you wanna see my function that converts the numb to a string here it is:

//////////////////////      Numbs_Digits    ////////////////////////////////////////////////
template<typename T>
int Numbs_Digits(T numb, int scope)
{
    stringstream ss(stringstream::in| stringstream::out), ss2(stringstream::in| stringstream::out);
        unsigned long int length= 0;
        unsigned long int numb_wholes;
                          ss2 << (int)numb; 
                          numb_wholes = ss2.str().length(); ss2.flush();
        bool all= false;
        ss.precision(11);   // HOW DO I MAKE THE PRECISION NUMBER THE NUMBER OF DECIMALS?

    switch(scope){
        case ALL:        all = true;

        case DECIMALS:   ss << fixed << numb;
                         length += ss.str().length()- (numb_wholes +1); // +1 for the "."
                         if(all!= true) break;

        case WHOLE_NUMBS: 
                         length += numb_wholes;
                         if(all!= true) break;

        default: break;}

        return length;};
Griffin
  • 2,399
  • 7
  • 48
  • 83
  • 3
    Instead of giving us your sample code, it might be better if you could give a clearer description of the problem. Please gives some examples of numbers and the results you expect. – Aaron McDaid May 16 '11 at 01:37
  • 2
    Judging from your previous questions, I'd recommend reading up on how floating point actually works. Then you will understand why "the number of decimals in a double" doesn't make much sense. – hammar May 16 '11 at 01:44
  • Possible dupe of http://stackoverflow.com/questions/4738768/printing-double-without-losing-precision – ThomasMcLeod May 16 '11 at 03:42

3 Answers3

3

If you want to know the maximum number of decimal digits that a long double can store, this value is available in the constant LDBL_DIG defined in cfloat. Note that this number is actually an approximation as the values are stored in binary internally, and thus the range of values is not a power of 10.

jwodder
  • 54,758
  • 12
  • 108
  • 124
2

Only some decimal numbers can be stored in exact form as a floating point number. Because of this there is no way to determine how many decimal places are significant for any decimal number for which this is not true. As hammar suggested, read up on the floating point storage format, I believe that every programmer should have some knowledge of low level stuff like this :D

multiplying by a really large number like 10 billion doesn't work because of the innacurate way computers store decimals, making 2.3 2.2999575697

This is exactly the problem. Would you be able to look at 2.999575697 and tell me it has two decimal places? This number is an example of a decimal number that cannot be stored in exact form using the floating point format. The best you could do is count the significant decimal places stored in the floating point number that best approximates the original decimal number it was given - which I can't imagine would be much use.

Edited for a more accurate explanation.

Snyex
  • 91
  • 6
  • 1
    Binary floating point numbers _are_ an exact representation, it's just that there are some fractions that can be expressed with a finite decimal, but not with a finite binary. – phoog May 16 '11 at 02:39
  • A double holds a number 52 signifcant binary digits or 15.95 significant decimal digits (53 log 2). However, up to 17 decimal digits are necessary to distinguish adjacent double values. – ThomasMcLeod May 16 '11 at 03:40
  • 1
    @phoog I should have used the wording: floating point numbers cannot be an exact representation of some decimal numbers. This being an issue that would come up often enough in any solution to this problem that not much would be gained from attempting such. – Snyex May 16 '11 at 04:05
  • @Synex: Stack Overflow won't allow me to remove my downvote unless you edit your answer. If you do so, I'll remove it. – phoog May 16 '11 at 19:28
0

Can you not set the ios_base precision to the maximum number of decimal digits in the significand on your platform in cfloat.h, and then, using ios_base::setf(), change the floating point formatting to scientific, which will remove any trailing zeroes from the floating point number (you'll just have to trim the exponent off the end)?

Jason
  • 31,834
  • 7
  • 59
  • 78