Printf problem with big numbers....

P

pasukaru

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000


Can anybody tell me WHY ?
 
P

P.J. Plauger

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000


Can anybody tell me WHY ?

Because that's what you told it to do, with the %lf conversion
specifier. Try %lg to get rid of the trailing fraction.

And, oh yes, a typical double these days holds only 53
fraction bits, so everything after decimal digit 16 or
thereabouts is nonsense. If you have a compiler that
uses 80-bit IEEE for long double, you can get your 64-bit
integers. SPARC has a long double with a 113-bit fraction.
But some compilers, like the VC++ family, use the same
representation for both double and long double, so you'll
have to settle for 53.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
D

David Sampson

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000


Can anybody tell me WHY ?
Try specifying a decimal expansion. %lf.9 should force it to be precise
to nine decimal places. I don't remember the exact syntax, but I
believe it looks something like that. It's probably only storing 16
bytes, and rounding the rest for simplicity. Correct me, but the syntax
should go something like that.

David Sampson
 
S

slebetman

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000

First off, depending on your machine and compiler, and assuming that a
double is 64 bits, you need to realise that the fraction part of the
double, that is the 'number' part is only 52 bits. This means that the
'number' part of the double is only 2^52 which is much, much smaller
than your required 2^64. 2^52 has a maximum of only 16 significant
digits (compared to a maximum of 20 significant digits of 2^64). So
your number, when represented as a double, have only 16 significant
digits, which is what you got in the result. The problem is not with
printf(). The problem is that you used double.

If you really want very large integers, then use "long long" (that's
two longs) which is at minimum 64 bits. That will give you a maximum
integer value of (2^64)-1. If you insist on using double then be
prepared to live with 12(or 11 depending on the number) significant
digits.
 
J

Joe Wright

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000


Can anybody tell me WHY ?

Maybe. On most of our systems today, 'double' is a 64-bit object with a
53-bit mantissa which will translate to around 16 digits of rational
decimal representation. And that's what you got.

Perhaps 'double' is not what you want. There are some 'bignum' libraries
out there which allow arbitrarily large numbers. Google is your friend.
 
J

Jordan Abel

I need to work with big integers ( around 2.^64), and I know it is not
possible to have integer that big.
So I use "double" instead, and it is ok with calculations.
But when I want to print them on screen or in a file, there is a
problem...

For example, if I write this :

printf("result: %lf\n",1234567890987654321.012345678);

What I get on cmd is:

result: 1234567890987654400.000000

try %.0f to get rid of the extra decimal digits

[incidentally, %lf is not needed for printf, and is forbidden in c89]
Can anybody tell me WHY ?

your doubles aren't precise enough? try long double maybe [though that's
not guaranteed to be better]
 
P

pasukaru

well, the problem was that these numbers are supposed to be converted
in base 2 later on....
So if 1234567890987654321 becomes 1234567890987654400, that is a big
problem isn't it ?
But I changed my algorithm, and now work directly in base 2, and
instead of printing the results in base 10 or base 2, I just print the
bits that are == 1 (for example x0x3x5x45x62), so now I deal with
strings, and not with numbers, so it is ok.

Thank U all for your help.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top