Results 1 to 15 of 15

Thread: RETARD QUESTION >> Length of an integer?

  1. #1

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815

    RETARD QUESTION >> Length of an integer?

    Hi,

    How do i return the number of digits in an integer? For example, return 5 from 24318, or 7 from 9213214.

    Thanx
    Last edited by Illspirit; Dec 6th, 2001 at 02:42 PM.
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  2. #2
    If you mean int which can only have 5 digits you can do an if statement like below. If you mean long or any integer, you can convert the number to a string and get the number of characters.

    Code:
    int digitsInInt(const int &x)
    {
        if (x < 10) return 1;
        if (x < 100) return 2;
        if (x < 1000) return 3;
        if (x < 10000) return 4;
        else return 5;
    }

  3. #3

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Thanx

    so simple, i dunno why i didnt think of that
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  4. #4
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Or if you want it to work for any length...

    If your number is n: int(log10(n)) + 1. This will always return the length of any number regardless of its size. I am not sure if that syntax works in c++, becuase I dont' know what the command is for log base 10, but you get the idea. Hope this helps.
    Joe

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I was going to post how you could do this with integer division assign, but now i realise how filly has already beaten me in performance never thought that would happen, congrats filburt
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Originally posted by filburt1
    If you mean int which can only have 5 digits you can do an if statement like below. If you mean long or any integer, you can convert the number to a string and get the number of characters.

    Code:
    int digitsInInt(const int &x)
    {
        if (x < 10) return 1;
        if (x < 100) return 2;
        if (x < 1000) return 3;
        if (x < 10000) return 4;
        else return 5;
    }
    this code = slooooow

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Nope, filburt has found the simplest and most efficient way of doing it. Yet the MSVC6 compiler isn't really smart to flip the order of the jmp's so you need to modify it a bit:
    PHP Code:
    inline int digitsInInt(const int &x)
    {
        if (
    10)
            if (
    100)
                if (
    1000)
                    if (
    10000)
                        return 
    5;
                    else return 
    4;
                else return 
    3;
            else return 
    2;
        else return 
    1;

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hmm, that one did jump to a later part in the code which i didn't notice, this should do the trick:
    PHP Code:
    inline int digitsInInt(const int &x)
    {
        
    int y;
        
    y=1;
        if (
    >= 10){
            
    y=2;
            if (
    >= 100){
                
    y=3;
                if (
    >= 1000){
                    
    y=4;
                    if (
    >= 10000){
                        
    y=5;
                    }
                }
            }
        }
        return 
    y;

    compiles to this:
    Code:
    	mov	ecx, DWORD PTR _a$[esp+4]
    	mov	eax, 1
    	cmp	ecx, 10					; 0000000aH
    	jl	SHORT $L2271
    	cmp	ecx, 100				; 00000064H
    	mov	eax, 2
    	jl	SHORT $L2271
    	cmp	ecx, 1000				; 000003e8H
    	mov	eax, 3
    	jl	SHORT $L2271
    	cmp	ecx, 10000				; 00002710H
    	mov	eax, 4
    	jl	SHORT $L2271
    	mov	eax, 5
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    use my code

    Not only is it not limited to 5 digits numbers, it is one line of code, adn it executes quite quickly. int(log base 10(n) ) + 1. Very simple code that works very well.
    Joe

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You can put any amount of digits in, just extend.
    approximating logaritms are very expensive operations.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    EH it basically comes down to...

    If you want an algorithm, you have to use mine. Otherwise, you can use the other method, although I am not partial to writing more lines of code for something that has only a miniscule effect on the system.

    Joe

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    both are algoritms, and you don't have to rewrite an already written algoritm, wheter you advocate functional or object oriented programming.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131
    Hello,
    This code should work with any integer.

    PHP Code:
        int x 10;
    int NumberOfDigit 1;
    for(
    Number;Number>x;x*=10)
    {
        
    NumberOfDigit++;

    Khavoerm Irithyl

  14. #14
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Yep that last post is the way to go if...

    If you do not want ot use logs, the last post is right. It is a complete algorithm. The problem with the earlier is more lines of code and it is not complete because it can not handle any number.

    Joe

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Actually I was inteding to do similar with integer division assign, but i did realize filburt got the simplest and most efficient algoritm:
    The largest integer possible is
    4 294 967 296
    meaning results value can be between 1-10 making an extension of 10 of filburt's algoritm complete.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width