开发者

How to figure out how many decimal digits are in a large double?

开发者 https://www.devze.com 2023-03-06 07:15 出处:网络
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:

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 functio开发者_如何学Cn 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;};


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.


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.


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)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号