Determine the size of long

Jan 28, 2011 at 12:24pm
1
2
3
4
5
6
7
8
long tmp = 1;
int size_of_tmp_long_variable would be 1

long tmp = 12;
int size_of_tmp_long_variable would be 2

long tmp = 123;
int size_of_tmp_long_variable would be 3


Is there C++ function for that?

Best regards,
Peter
Jan 28, 2011 at 12:27pm
use cmath and log10.


1
2
3
4
5
#include <cmath>

// inside function:
long num = 29;
int length = (num == 0) ? 1 : log10(num) + 1;


This has flaws. No negative numbers, and it doesn't work with decimals.
Jan 28, 2011 at 12:27pm
The size of a long always remains the same as long as you compile it the same way. You can use the sizeof operator to get the size of a datatype (or variable).

Oh, you meant like the number of characters? Sorry, misunderstood you. Like above.
Last edited on Jan 28, 2011 at 12:28pm
Jan 28, 2011 at 12:47pm
you can convert it to a string (like itoa) and use the size of that string
Jan 28, 2011 at 12:53pm
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cmath>
#include <sstream>

int digit_count(long int number)
{
    if (number == 0) return 1;
    int count = 0;
    while( number != 0) 
    {
        number /= 10;
        ++count;
    }
    return count;
}

int digit_count_Wolfgang(long int number)
{
    return (number == 0)? 1: (int) log10((double)abs(number)) +1;;
}

int digit_count_coder777(long int number)
{
    std::stringstream stream;
    stream << abs(number);
    return stream.str().size();
}

int main ()
{
    long a_number =  -123456789;
    int number_of_digits = digit_count(a_number);
    int number_of_digits2 = digit_count_Wolfgang(a_number);
    int number_of_digits3 = digit_count_coder777(a_number);

    std::cout << "The number of digits in " << a_number << " is " << number_of_digits << std::endl;
    std::cout << "The number of digits in " << a_number << " is " << number_of_digits2 << std::endl;
    std::cout << "The number of digits in " << a_number << " is " << number_of_digits3 << std::endl;
    
    return 0;
}
Last edited on Jan 28, 2011 at 1:04pm
Jan 28, 2011 at 3:59pm
This is the simplest way I can think of:

1
2
3
4
5
6
7
8
9
#include <iostream>

int main(int argc, char* argv[])
{
int x = 5467;
int size = sizeof(x) / sizeof(int);
std::cout << "x is " << size << " long." << std::endl;
return 0;
}


Size will first get the size of x, which is 32 bits, then divide by the size of an integer, which is 8. So the size will be 4.
Jan 28, 2011 at 4:09pm
sizeof(int)/sizeof(int) will always be 1...
Jan 28, 2011 at 4:19pm
closed account (z05DSL3A)
packetpirate,

Although it is badly worded, it would appear that the OP wants to know how many digits there are in the number held in a particular long variable.

As Bazzy says your code would always give 1 for the size.
Jan 28, 2011 at 5:08pm
Read it again...
It does not say:

 
sizeof(int) / sizeof(int);


It says:

 
sizeof(x) / sizeof(int);
Jan 28, 2011 at 5:12pm
x is an int
Jan 28, 2011 at 5:12pm
Nevermind. I read that wrong.
I guess that method only works with arrays.
Last edited on Jan 28, 2011 at 5:14pm
Jan 28, 2011 at 5:17pm
1
2
3
4
5
6
7
8
9
int x = 123;

int digits = 0;
while( x > 0 ) {
    ++digits;
    x /= 10;
}

cout << digits << endl;


(untested...)

EDIT: Already implemented above by Grey Wolf.
Last edited on Jan 28, 2011 at 5:19pm
Jan 29, 2011 at 3:28am
Here's my completely inappropriate for the beginner forum answer, using the Boost Conversion library:
size_t size_of_tmp_long_variable = boost::lexical_cast<std::string>(abs(tmp)).size();

http://www.boost.org/doc/libs/1_43_0/libs/conversion/lexical_cast.htm

Jan 29, 2011 at 4:13am
@PanGalactic

My brain just exploded...
Jan 29, 2011 at 4:38am
@packetpirate

The brief explanation:

A lexical_cast is a conversion from numeric to string representation or vice versa.

1
2
3
boost::lexical_cast<std::string> // Convert the next parameter (see next line) to a string.
(abs(tmp))                       // The absolute value of the tmp numeric variable (same that everyone else is doing).
.size()                          // Return the size of the string. (The string itself is discarded.) 


It is doing essentially what Grey Wolf's digit_count_coder777() function does but using a lexical_cast instead of a stringstream.
Topic archived. No new replies allowed.