numeric header - number of digits

This is a discussion on numeric header - number of digits within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a buffer to be filled by sprintf() in order to convert an unsigned long to a ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435

    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?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,796
    >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
    Portugal
    Posts
    7,435
    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?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,796
    >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
    Portugal
    Posts
    7,435
    Most excellent! Thanks
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21