Print a number of size 19 digit

B

Bhanu

I have a problem in a piece of C Program.
I have to print a number of size 19 digit (No decimals).
What data type should I use to store and print this no.
I have already tried using double, long double but in vain. I also
think that it might have to do with our environment setting but do not
know what to change. I searched in the new but was not lucky.

Requirement:

Actually, there is a hexadecimal number. That needs to be changed to a
whole number, then it has to be stored in a variable, then some
processing needs to be done on that , and finally it needs to be
printed on screen.

So, it seems long double can store the number. But, prints it in 23e25
kind of format, whereas what we want is all digits (like
235876273856346783)
 
M

Minti

Bhanu said:
I have a problem in a piece of C Program.
I have to print a number of size 19 digit (No decimals).
What data type should I use to store and print this no.
I have already tried using double, long double but in vain. I also
think that it might have to do with our environment setting but do not
know what to change. I searched in the new but was not lucky.

In standard C, you can't do that. Search for "bignum" using your search
engine. If you don't require the highly generalized format of these
libraries, you can create your own. The basic aspect is that you take
and deal with them in a way you would deal with BCD, Binary Coded
Decimal.

HTH
 
K

Keith Thompson

I have a problem in a piece of C Program.
I have to print a number of size 19 digit (No decimals).
What data type should I use to store and print this no.
I have already tried using double, long double but in vain. I also
think that it might have to do with our environment setting but do not
know what to change. I searched in the new but was not lucky.

Requirement:

Actually, there is a hexadecimal number. That needs to be changed to a
whole number, then it has to be stored in a variable, then some
processing needs to be done on that , and finally it needs to be
printed on screen.

So, it seems long double can store the number. But, prints it in 23e25
kind of format, whereas what we want is all digits (like
235876273856346783)

You can print it that way if you use the appropriate format
specifiers, but long double may not be able to represent a full 19
digits of precision (just over 63 bits).

You probably need to use some kind of multiple precision integer
package. (GNU MP might be a possibility.)
 
C

Chris Croughton

I have a problem in a piece of C Program.
I have to print a number of size 19 digit (No decimals).
What data type should I use to store and print this no.

There may not be one. On my machine (x86) a long double has just short
of 19 decimal digits (LDBL_EPSILON is 1.08e-19). You can use the
following program to display all of the values defined for float.h (the
actual values -- assuming you can even find the header -- are often
obscure, C90 says that most of them may not even be constant
expressions, only FLT_RADIX has to be a constant expression):

#include <stdio.h>
#include <float.h>

int main(void)
{
printf("FLT_RADIX %d\n", FLT_RADIX);
printf("\n");
printf("FLT_MANT_DIG %d\n", FLT_MANT_DIG);
printf("FLT_DIG %d\n", FLT_DIG);
printf("FLT_MIN_EXP %d\n", FLT_MIN_EXP);
printf("FLT_MAX_EXP %d\n", FLT_MAX_EXP);
printf("FLT_MIN_10_EXP %d\n", FLT_MIN_10_EXP);
printf("FLT_MAX_10_EXP %d\n", FLT_MAX_10_EXP);
printf("FLT_MIN %.*g\n", FLT_DIG, FLT_MIN);
printf("FLT_MAX %.*g\n", FLT_DIG, FLT_MAX);
printf("FLT_EPSILON %.*g\n", FLT_DIG, FLT_EPSILON);
printf("\n");
printf("DBL_MANT_DIG %d\n", DBL_MANT_DIG);
printf("DBL_DIG %d\n", DBL_DIG);
printf("DBL_MIN_EXP %d\n", DBL_MIN_EXP);
printf("DBL_MAX_EXP %d\n", DBL_MAX_EXP);
printf("DBL_MIN_10_EXP %d\n", DBL_MIN_10_EXP);
printf("DBL_MAX_10_EXP %d\n", DBL_MAX_10_EXP);
printf("DBL_MIN %.*g\n", DBL_DIG, DBL_MIN);
printf("DBL_MAX %.*g\n", DBL_DIG, DBL_MAX);
printf("DBL_EPSILON %.*g\n", DBL_DIG, DBL_EPSILON);
printf("\n");
printf("LDBL_MANT_DIG %d\n", LDBL_MANT_DIG);
printf("LDBL_DIG %d\n", LDBL_DIG);
printf("LDBL_MIN_EXP %d\n", LDBL_MIN_EXP);
printf("LDBL_MAX_EXP %d\n", LDBL_MAX_EXP);
printf("LDBL_MIN_10_EXP %d\n", LDBL_MIN_10_EXP);
printf("LDBL_MAX_10_EXP %d\n", LDBL_MAX_10_EXP);
printf("LDBL_MIN %.*Lg\n", LDBL_DIG, LDBL_MIN);
printf("LDBL_MAX %.*Lg\n", LDBL_DIG, LDBL_MAX);
printf("LDBL_EPSILON %.*Lg\n", LDBL_DIG, LDBL_EPSILON);

return 0;
}

On my system that gives:

FLT_RADIX 2

FLT_MANT_DIG 24
FLT_DIG 6
FLT_MIN_EXP -125
FLT_MAX_EXP 128
FLT_MIN_10_EXP -37
FLT_MAX_10_EXP 38
FLT_MIN 1.17549e-38
FLT_MAX 3.40282e+38
FLT_EPSILON 1.19209e-07

DBL_MANT_DIG 53
DBL_DIG 15
DBL_MIN_EXP -1021
DBL_MAX_EXP 1024
DBL_MIN_10_EXP -307
DBL_MAX_10_EXP 308
DBL_MIN 2.2250738585072e-308
DBL_MAX 1.79769313486232e+308
DBL_EPSILON 2.22044604925031e-16

LDBL_MANT_DIG 64
LDBL_DIG 18
LDBL_MIN_EXP -16381
LDBL_MAX_EXP 16384
LDBL_MIN_10_EXP -4931
LDBL_MAX_10_EXP 4932
LDBL_MIN 3.36210314311209351e-4932
LDBL_MAX 1.18973149535723177e+4932
LDBL_EPSILON 1.08420217248550443e-19
I have already tried using double, long double but in vain. I also
think that it might have to do with our environment setting but do not
know what to change. I searched in the new but was not lucky.

You may have to write your own, or use something like gmp (the GNU
MultiPrecision library). However, if the number is always positive and
you are using a C99 compiler (or one implementing C99 features, like
gcc) then you could use unsigned long long, which is guaranteed to be at
least 64 bit which just fits 19 decimal digits.
Requirement:

Actually, there is a hexadecimal number. That needs to be changed to a
whole number, then it has to be stored in a variable, then some
processing needs to be done on that , and finally it needs to be
printed on screen.

Is it hexadecimal floating point? If it's integer, see the preceding
paragraph.
So, it seems long double can store the number. But, prints it in 23e25
kind of format, whereas what we want is all digits (like
235876273856346783)

You could use

printf("%19.0Lf", value);

The .0 says "print the floating point number with no fractional digits,
and no decimal point either" (note the L modifier to tell it to expect a
long double).

For unsigned long long you would need to use format %llu.

Chris C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,172
Latest member
NFTPRrAgenncy
Top