Thread: strlen for nummeric types?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    strlen for nummeric types?

    Please imagine size is a variable and could be also a long double with 2.000.000.
    Code:
    	int size = 50;
    	char size_as_chararray[8000];
    	sprintf(size_as_chararray, "%d", size);
    How to allocate the size of the char array more elegant? I think for allocating exactly what it needs I would need to know the length of variable size.

    I need something like
    int size = 100.000
    strlen(size) --> 6 (Numbers)

    double size = 555555555
    strlen(size) --> 9

    Well, obviously I can't covert into char array first and use strlen then because converting into char array is the thing I originally wanted to do.

    Is there some function to get the amount of numbers of a numeric type?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes: 1+floor(log10(num)) is the number of decimal digits in the whole-number part of num. (So that's why your 11th grade teacher went on and on and on about logs. Now we know.)

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    #include <float.h> /** limit constants for floating points **/
    
    char text[LDBL_DIG]; /** at least 10, the most you ought to need **/
    int d = foo();
    
    sprintf(text, "&#37;d", d);

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by tabstop View Post
    Well, yes: 1+floor(log10(num)) is the number of decimal digits in the whole-number part of num.
    And what if num is negative ? Or num is zero ?

    A "strlen"-like function for integer would make sense because there's basically only one notation for them, but it's a bit different for floating point numbers where scientific notation is used.

    So, to solve your problem, declare an array of around 50 characters, use only scientific notation for floating-point number and you shouldn't have any problem with your code for at least a couple of decades.
    I hate real numbers.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by foxman View Post
    And what if num is negative ? Or num is zero ?
    Throw away the i*pi part and just use the real part of the logarithm . And now that there's only one special case, it's easy to check for zero.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A "strlen"-like function for integer would make sense because there's basically only one notation for them,
    C++ allows three different notations for integer literals. And it doesn't even support binary.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you need to do this a lot, there's a much more efficient method which takes at most 4 comparisons.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM