Thread: length of an integer

  1. #1
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942

    length of an integer

    how do I find an integer length? in pseudocode:

    int whatup=100;
    length=intlth(whatup);
    //length now equals 3

    is that possible?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Teh most direct method is to calculate the floor of the log base 10 of the number (you can find the appropriate functions std::floor, and std::log10 in <cmath>).

    You can also do it by dividing by ten (and incrementing a counter) until the value equals zero (since its integer arithmetic).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    all right! thanks!

  4. #4
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Originally posted by Zach L.
    Teh most direct method is to calculate the floor of the log base 10 of the number (you can find the appropriate functions std::floor, and std::log10 in <cmath>).
    That doesn't work, does it? Log(87), for example, is ~1.94. floor(1.94) is 1. So you'll need to do what he said, and then add 1. But instead of using floor, can't you just cast it to an int and let the decimals truncate?
    Away.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Oh yeah... forgot that little detail.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-24-2007, 05:28 PM
  2. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. Trying To Implement A 'Player' Class
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 03:51 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM