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

Home Posts Topics Members FAQ

problem to print large double number. Help!

When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=89999999999 9.894400;
printf("%lf\n", num);

The output is: 899999999999.89 4409

Could anyone tell me what the problem is? How can I get the right
output?

Thanks.
Nov 13 '05 #1
4 21693
ai lian wrote:

When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=89999999999 9.894400;
printf("%lf\n", num);

The output is: 899999999999.89 4409

Could anyone tell me what the problem is? How can I get the right
output?


Question 14.1 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

covers part of your problem. The rest is due to unrealistic
expectations: Why do you think you're going to get accuracy
to eighteen (or more!) significant digits? The C language
Standard only promises ten and you're actually getting
fifteen, so what are you complaining about? ;-)

--
Er*********@sun .com
Nov 13 '05 #2
ai lian wrote:
When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=89999999999 9.894400;
printf("%lf\n", num);

The output is: 899999999999.89 4409

Could anyone tell me what the problem is?
Yes. Usual implementations of floating-point numbers don't have that
much accuracy; furthermore there are numbers they cannot express
accurately at all, unless you happen to have a machine with unbounded
memory.

You're complaining about an error of 9 parts in 10e18 (or 17 or 19,
I can't count very well, 9 in a billion billion. Have some perspective.
(Also, see the FAQ.)
How can I get the right output?


Use strings to hold decimal numbers?

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3

"ai lian" <ai********@yah oo.com> schrieb im Newsbeitrag
news:9b******** *************** ***@posting.goo gle.com...
When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=89999999999 9.894400;
printf("%lf\n", num);

The output is: 899999999999.89 4409

Could anyone tell me what the problem is? How can I get the right
output?

Maybe you could try changing the original input a little bit hoping to
obtain a result positioned nearer to the desired value. Or use two
numbers (one before and one after the digit)?
Thomas Bonner

Nov 13 '05 #4

"ai lian" <ai********@yah oo.com> wrote in message
news:9b******** *************** ***@posting.goo gle.com...
When I use printf to print a large double number, the result is not
the same as the original input number. For example:

double num=89999999999 9.894400;
printf("%lf\n", num);

The output is: 899999999999.89 4409

Could anyone tell me what the problem is? How can I get the right
output?


Normally %f is used for output, and not %lf.

In most implementations , and for most values, %f prints a reasonably number
of digits to round to the expected value. You seem to have found a case
where it doesn't.

Note that Java, for example, always prints enough digits so that when
converted back to internal format the same binary value is obtained. This
often means that the printed output disagrees with the user expectation, but
C doesn't normally do that.

Anyway, you got more digits than is normal for a double.

-- glen
Nov 13 '05 #5

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

Similar topics

1
20548
by: Ryoga | last post by:
How can I print out the value of a large double in plain decimal form without the exponential notation? For example (quick code): PlainPrint.java: --------------- public class PlainPrint { public static void main(String args) { double aValue = 123456789.1234; System.out.println(aValue); }
2
632
by: gouqizi.lvcha | last post by:
Hi, All: I wonder aht is the smallest positive double number in C++ under 32-bit CPU Rick
6
2136
by: John Baker | last post by:
Hi: As those who have looked at this newsgroup recently will realize, I am a neophyte with Access, although I have experienced with Approach (the Lotus product). There are things I could easily do in Approach that appear to require some subtle and complex manipulation in Access. Herein lies my present problem. I have a number of tables, including one called "tblPO", which contains purchase orders. Some of the details of these change...
1
2992
by: LBX | last post by:
I am looking for a large random number generator or ways to take smaller random numbers into large ones. The target size of these random numbers is 512 bits. Any links or codes are nice as I am having trouble using the search engines to find it. So please do not recommend Google. ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+...
2
2765
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output 0.220 it comes out as 0.22 and drops the zero. Also, outputting 1.000 outputs as 1. How can I format it to include the zeros?
2
2472
by: Basel | last post by:
Hi All! I'm having a problem with a growing number of connections. my app generates http requests and reads responses. the app creates large number of threads (100 or more) that generates the requests, this is the code: ServicePointManager.DefaultConnectionLimit = 10; ThreadFunction()
11
22791
by: shiniskumar | last post by:
Ive got a double number. double n=47758.35; Inorder to get the decimal part of this number i did as follows long decPart = (long) ((n-Math.floor(n))*100); in most cases i get the correct decimal part but in this case the decPart becomes"34" instead of "35" how can i get the exact decimal part
4
42736
by: b4ukiran | last post by:
Hi all, I have a requirement to print large double values without the exponential notaion. The double value can be a declared variable or any calculated value within the code. In any case, I should be able to print the result without containing the exponential notation. Please help me out.
6
2711
by: arashmath | last post by:
Hi everyone, I have a problem with large dynamic memory allocating. this is my code (in briefness): #include <stdio.h> #include <mem.h> #include <assert.h> #define SML_BOUNDS_CHECK
0
8305
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
8823
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...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6163
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.