473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printf problem with big numbers....

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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.000000
Can anybody tell me WHY ?

Jan 24 '06 #1
6 3553
<pa******@gmail .com> wrote in message
news:11******** ************@z1 4g2000cwz.googl egroups.com...
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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.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
Jan 24 '06 #2
pa******@gmail. com wrote:
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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.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
Jan 24 '06 #3
pa******@gmail. com wrote:
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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.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.

Jan 24 '06 #4
pa******@gmail. com wrote:
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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 24 '06 #5
On 2006-01-24, pa******@gmail. com <pa******@gmail .com> wrote:
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",12345678 90987654321.012 345678);

What I get on cmd is:

result: 123456789098765 4400.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]
Jan 24 '06 #6
well, the problem was that these numbers are supposed to be converted
in base 2 later on....
So if 123456789098765 4321 becomes 123456789098765 4400, 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.

Jan 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1868
by: Paul | last post by:
The following code: <?php header("Content-Type: text/plain"); $numbers = array(3.714, 0.7142857142857143, 9.667, 6.333, 7, 6, 8, 2, 3, -2, 0, -6.6, 2.25, 4.333, -8, 3, 3.141592654, 5.5, 6.5, 6.857); foreach ($numbers as $value) printf("% 20.2f\n", $value); ?> produces the following output:
18
5940
by: Graham Nicholls | last post by:
Hi. I'm having some fun with numbers. I've extraced an image sizes from a jpeg file img_x,img_y=image.getsize() then I'm trying to use those sizes to scale the image, but because python has decided that they are integers, I keep getting division by zero errors
5
5286
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion specification for each column (e.g. printf(%10s %25s %15s\n", pszCol1, pszCol2, pszCol3)). My problem is that when a string is longer the column's width, it overflows the column and takes the table out of alignment. What I want it to do is word-wrap...
11
5933
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
33
5896
by: Kenneth Brody | last post by:
I know that passing printf() too few arguments, or arguments of the wrong type invokes UB. However, what about passing too many arguments, if the expected arguments are of the correct type? For example: char format1 = "foo %s bar %s baz %d"; char format2 = "foo %s bar %s no baz here"; char *strvar1 = "one", *strvar2 = "two";
2
2437
by: Beffmans | last post by:
Hi I want to make a regularexpressionvalidator for a textbox to allow only numbers but not a specified number of digits How to do this? ch B *** Sent via Developersdex http://www.developersdex.com ***
5
2958
by: Sebastian Araya | last post by:
Hello, I'm trying to write a rotational symbol in a CLI process, to report activity to the user console, using printf( "%s\b", symbol ); but I see the control character (\b) is been printed out instead of backspacing. Is there any work around ? Thanks in advance,
5
1674
by: sesling | last post by:
I have a database field that stores 8 and 9 digit values. I need to calculate the sum value using the first 8 digits. ex.of stored numbers 123456781 234567892 45678903 987654321 calculation should use 12345678 23456789
7
10999
by: Avaenuha | last post by:
Hi, It appears my program can't get past a particular printf() statement. Code excerpt: printf("Sales Report\n--------------"); printf("Testing code - pre loop entry"); while(category != null)
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.