Thread: Max Digits

  1. #1
    Unregistered
    Guest

    Max Digits

    Hi,
    I am writing a C program that will allow me to determine the maximum number of digits that a value can have to be within the range 0..m.

    for example:

    if m=23
    max number of digits =1
    Actual range 0..9

    if m=379
    max number of digits =2
    Actual range 0..99

    if m=453638923
    max number of digits =8
    Actual range 0..99999999

    where 9 is a digit not a value...

    firstly I dont want code but could someone explain the above theory as I just dont get it....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    log10(23) = 1.xxx
    log10(379) = 2.xxx

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I dont understand. Isn't the value 20 within the range of 0...23? 20 has 2 digits.

    What you need to do to find out the max digits is to find the number of digits your 'm' has. I really dont know how to explain theory, but I can give you an example...


    12345 / 10 = 1234.5
    12345 / 100 = 123.45
    12345 / 1000 = 12.345
    .
    .
    .

    remember that if both values are ints, then the fractional portion of the values will get truncated (cut off)

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Salem made use of the logarithm function, it is in some way the inverse of the power function. Example:

    10^2 = 100
    log10 (100) = 2

    Note that the base is 10, that's why it goes OK. For numbers with base 10, the total number of digits can be calculated as:

    nr_of_digits = (int) log10 (N) + 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Find max profit among input salary
    By hlam in forum C Programming
    Replies: 8
    Last Post: 11-16-2008, 10:30 AM
  3. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  4. Overwriting all in array, why?
    By guesst in forum C Programming
    Replies: 7
    Last Post: 10-09-2008, 05:56 PM
  5. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM