1

hi I have 2 question the first one is the one in the title and the other one is here: does unsigned long long is the biggest integer (can hold the biggest amount of characters)? cause I need an int that can hold few millions characters (digits) is this possible? I'm coding in C. and this is connecting me to the other question how can I display the amount of digits on the screen? is it need to be like this?:

printf("%d", intName.length)

thanks every one!!

NSH
  • 23
  • 1
  • 6
  • `unsigned long long` having range only `0` to `2^64 - 1`. (`18,446,744,073,709,551,615`) – haccks Dec 30 '13 at 23:13
  • 3
    long long is not gonna cut it. You need a library like gmplib. http://en.wikipedia.org/wiki/GNU_Multiple_Precision_Arithmetic_Library – Tesseract Dec 30 '13 at 23:14
  • Why do you need numbers that have millions of digits? Just wondering, since you "didn't understand nothing" of a pretty good answer, which makes me perplexed about what you might be trying to do. – Andrey Mishchenko Dec 30 '13 at 23:41
  • If you need a number like 100 digits you could allocate 100 bytes of memory and there to store 1 digit as 1 byte. You would need to implement your own add/sub/div/mul... function calculations. We were doing something like this in uni in Pascal, where we had to make sum of 2 mega-long floating point numbers. Was interesting for practicing and finding best solution. – Wiggler Jtag Dec 30 '13 at 23:45

4 Answers4

1

I am assuming that when you refer to the amount of characters you mean the number of digits in the number. If so then one this question has everything you need to know and included code similar to this

int numberOfDigits(unsigned long long n) 
{
    if (n == 0)
        return 0;
    return floor( log10( abs( n ) ) ) + 1;
}

as for holding a few million digits you probably want to look into using a library such as The GNU Multiple Precision Arithmetic Library which includes the function

size_t mpz_sizeinbase( const mpz_t op, int base )

which will tell you how many digits your number has.

Community
  • 1
  • 1
norlesh
  • 1,749
  • 11
  • 23
  • yes that what I mean but can you explain to me what is the meaning of this piece of code, because if I just copy past it to my program I didn't understand nothing!!! – NSH Dec 30 '13 at 23:30
  • from the inside out we have – norlesh Dec 30 '13 at 23:42
  • [sorry hit enter by mistake and got cutoff above] from the inside out we have `abs(n)` this is making sure the log10 function gets a positive number, `log10(...)` tells us how many powers of 10 fit into our number... so if n was 100 then `log10(100)` would be 2, or if n was 10000 then `log10(10000)` would be 4. Next is `floor(...)` which is just there to make sure that the result is an integer since the result of log10 was probably not a whole number. Lastly we add the `+ 1` to account for the last digit in the number such as the 3 in 13 since it isn't included in our count of powers of ten. – norlesh Dec 30 '13 at 23:54
  • didn't compile, didn't give you the correct result, ???? Have you included math.h ? – norlesh Dec 31 '13 at 00:17
  • and yes I did include math.h – NSH Dec 31 '13 at 00:23
  • yeh, thats not giving me enough to go on... first guess is theres a problem happening in the conversion from unsigned long long to the double used in the math.h functions... afraid your gonna have to hit the text books sorry – norlesh Dec 31 '13 at 00:44
  • @NSH you may forgot to put `-lm` option to include math library compiled binary – longtengaa Nov 08 '15 at 19:58
0
printf("%llu", xxxxxxxxx);

 the ll (el-el) long-long modifier with the u (unsigned) conversion

you can also use

 uint64_t a;
 uint32_t b;

But you need to inlcude inttypes.h library that gives you types such as int32_t, int64_t, uint64_t.

Tharanga Abeyseela
  • 3,255
  • 4
  • 33
  • 45
0

C99 provides intmax_t (and uintmax_t) which will be the largest supported integer type (typically 64 bit.)

Assuming you have a conforming C99 snprintf then you can get the number of digits with:

length = snprintf(NULL, 0, "%llu", value);

for an unsigned long long value (and %ju for uintmax_t.)

Otherwise you'll have to pass a buffer in (yuk) or do something manually like:

length = value < 10 ? 1 :
         value < 100 ? 2 :
         ...

also yuk!

But this is all pretty irrelevant if you really do want million-digit integers, in which case you'll need to use a library such as gmp to work with such big numbers.

Frank
  • 403
  • 2
  • 5
  • I'm new to C so can someone explain it in basic C language? – NSH Dec 30 '13 at 23:39
  • 1
    I suggest you get more comfortable with some basic C exercises then have a go solving your problem using the GMP library. – Frank Dec 30 '13 at 23:45
  • I already know the basics and the million digits number isn't the issue what I need is the "how-many-digits" show on the screen with a simple printf(); commend. can it be? – NSH Dec 30 '13 at 23:48
0

The maximal decimal length of an unsigned integer of a given type of bit length bitlen is given by 1 + floor(log10(2^bitlen-1)) (mathematically, without taking overflows and rounding errors into account). The approximation 1/log2(10) ~ 4004.0/13301 (obtained with continued fractions, see http://en.wikipedia.org/wiki/Continued_fraction) leads to the formula 1 + bitlen * 4004 / 13301 (computationally, i.e. the division rounds down). Mathematical details are given in the comments of the snippet below.

#include <limits.h>
#include <stdio.h>

/**
 * Maximal number of digits in the decimal representation of an unsigned type.
 *
 * floor( log2(2^bitlen - 1) / log2(10) ) == floor( bitlen / log2(10) )
 * otherwise an integer n would exist with
 *     log2(2^bitlen - 1) / log2(10) < n < bitlen / log2(10)
 *     log2(2^bitlen - 1) < n * log2(10) < bitlen
 *     2^bitlen - 1 < 2^(n * log2(10)) < 2^bitlen
 *     2^bitlen - 1 < (2^log2(10))^n < 2^bitlen
 *     2^bitlen - 1 < 10^n < 2^bitlen
 *     which is impossible
 *
 * 1 / log2(10) ~ 0.301029995663981
 * 4004 / 13301 ~ 0.30102999774453
 *
 *     1 + floor( log10(2^bitlen - 1) )
 *  == 1 + floor( log2(2^bitlen - 1) / log2(10) )
 *  == 1 + floor( bitlen / log2(10) )
 *  <= 1 + floor( bitlen * 4004.0 / 13301 )
 *  == 1 + bitlen * 4004 / 13301
 * with equality for bitlen <= 13300 == 8 * 1662.5
 */
#define DECLEN(unsigned_t) (1 + CHAR_BIT*sizeof(unsigned_t) * 4004 / 13301)

int main(int argc, char *argv[]) {
    printf("unsigned char      : %zu\n", DECLEN(unsigned char));
    printf("short unsigned     : %zu\n", DECLEN(short unsigned));
    printf("unsigned           : %zu\n", DECLEN(unsigned));
    printf("long unsigned      : %zu\n", DECLEN(long unsigned));
    printf("long long unsigned : %zu\n", DECLEN(long long unsigned));
    return 0;
}
Loic
  • 640
  • 4
  • 13