Thread: numeric header - number of digits

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    numeric header - number of digits

    I'm trying to create a buffer to be filled by sprintf() in order to convert an unsigned long to a string:

    From the documentation, I'm having trouble deciding if this is the correct size for the buffer:

    Code:
    char buff[numeric_limits<unsigned long>::digits + 1];
    Did I understand digits correctly?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Did I understand digits correctly?
    Yes and no. In this case, you missed that digits relies on radix, which is 2 for the basic integer types.

    Since you seem to want the decimal digits, you should be using digits10 instead of digits. However, this is a tricky area because digits and digits10 both say that they evaluate to the number of digits representable by the type "without change". In the case of digits10, you'll be getting the number of digits - 1 because the most significant digit is a troublemaker. Let's say that unsigned long is 32 bits. The largest value that can be held by a 32-bit unsigned type on your system is probably 4,294,967,295. That's ten digits. However, digits10 can't evaluate to 10 because that doesn't guarantee the value will be representable by an unsigned long (9,999,999,999 for example). So digits10 will evaluate to 9. That also doesn't take sign or other formatting characters into account. At the very least, your array will need to use digits10 and add 3: 1 for the null character, 1 for the most significant digit, and 1 for the sign.
    My best code is written with the delete key.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Very informative, Prelude. Thanks.

    But to make sure I understood, digits10 will evaluate to the maximum possible number of decimal digits without incurring in the error of being representable by a number higher than max()?

    Also, being this an unsigned long, is it still necessary to take into account the sign?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >digits10 will evaluate to the maximum possible number of decimal digits
    >without incurring in the error of being representable by a number higher than max()?
    That works.

    >Also, being this an unsigned long, is it still necessary to take into account the sign?
    Standard formatted output will ignore the sign for unsigned types (fancy that). However, application formatting might not see it in the same light and you would need the extra space. I like to err on the side of caution.
    My best code is written with the delete key.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Most excellent! Thanks
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits?
    By scatterice in forum C Programming
    Replies: 7
    Last Post: 05-12-2009, 01:30 PM
  2. decimal to number if digits in different bases
    By jorgejags in forum C Programming
    Replies: 21
    Last Post: 09-24-2008, 12:55 PM
  3. Count Number of Digits in and Integer
    By redneon in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2003, 04:16 PM