|
-
Dec 6th, 2001, 02:36 PM
#1
Thread Starter
Fanatic Member
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.
-
Dec 6th, 2001, 02:38 PM
#2
Member
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;
}
-
Dec 6th, 2001, 02:42 PM
#3
Thread Starter
Fanatic Member
Thanx
so simple, i dunno why i didnt think of that
-
Dec 6th, 2001, 02:49 PM
#4
Hyperactive Member
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
-
Dec 6th, 2001, 07:45 PM
#5
transcendental analytic
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.
-
Dec 6th, 2001, 08:19 PM
#6
PowerPoster
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
-
Dec 6th, 2001, 08:34 PM
#7
transcendental analytic
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 (x > 10)
if (x > 100)
if (x > 1000)
if (x > 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.
-
Dec 6th, 2001, 08:49 PM
#8
transcendental analytic
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 (x >= 10){
y=2;
if (x >= 100){
y=3;
if (x >= 1000){
y=4;
if (x >= 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.
-
Dec 6th, 2001, 10:18 PM
#9
Hyperactive Member
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
-
Dec 6th, 2001, 10:30 PM
#10
transcendental analytic
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.
-
Dec 6th, 2001, 11:00 PM
#11
Hyperactive Member
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
-
Dec 6th, 2001, 11:21 PM
#12
transcendental analytic
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.
-
Dec 7th, 2001, 08:20 AM
#13
Addicted Member
Hello,
This code should work with any integer.
PHP Code:
int x = 10;
int NumberOfDigit = 1;
for(Number;Number>x;x*=10)
{
NumberOfDigit++;
}
-
Dec 7th, 2001, 03:37 PM
#14
Hyperactive Member
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
-
Dec 7th, 2001, 04:08 PM
#15
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|