473,461 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Double troubles

Hi
There is code fragment:

BigDecimal bd = new BigDecimal("9999999999.999999");
System.out.println(bd.toString());

double d = 9999999999.999999D;
System.out.println(d);

DecimalFormat f = (DecimalFormat) NumberFormat.getInstance(Locale.US);
String s = f.format(d);
System.out.println(s);

f.setMinimumFractionDigits(6);
s = f.format(d);
System.out.println(s);

Here is the result:

9999999999.999999 - good result

9.999999999999998E9

10,000,000,000
~~~~~~~~~~~~~~~ precision loss

9,999,999,999.999998
~~~ precision loss
How can be avoided this precision loss in double ?
Jul 17 '05 #1
4 4410
Serhiy Brytskyy wrote:

<snip>
10,000,000,000
~~~~~~~~~~~~~~~ precision loss

9,999,999,999.999998
~~~ precision loss

How can be avoided this precision loss in double ?


You can't. Welcome to floating points.

Every primitive type has a finite range and precision. As it happens,
double cannot represent 9999999999.999999 exactly. It is therefore
rounded to the nearest value that double can accommodate. Of course,
this value is in binary, and it isn't exactly 9999999999.999998 either,
but 9999999999.999998 is in turn the nearest to this value to six
decimal places.

If you need the greater precision, then you've practically answered your
own question: use BigDecimal.

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
Jul 17 '05 #2
Ok, but see next:

C code

double d = 29.0 * 0.01;
printf("%f\n", d);
printf("%f\n", d * 100);

result:

0.290000
29.000000
Java code:
double d = 29.0 * 0.01;
System.out.println(d);
System.out.println((d * 100));

result

0.29
28.999999999999996
So, can we come to a conclusion that Java has buggy procedures with
floating point?
Stewart Gordon wrote:
Serhiy Brytskyy wrote:

<snip>
10,000,000,000
~~~~~~~~~~~~~~~ precision loss

9,999,999,999.999998
~~~ precision loss

How can be avoided this precision loss in double ?

You can't. Welcome to floating points.

Every primitive type has a finite range and precision. As it happens,
double cannot represent 9999999999.999999 exactly. It is therefore
rounded to the nearest value that double can accommodate. Of course,
this value is in binary, and it isn't exactly 9999999999.999998 either,
but 9999999999.999998 is in turn the nearest to this value to six
decimal places.

If you need the greater precision, then you've practically answered your
own question: use BigDecimal.

Stewart.

Jul 17 '05 #3
On Thu, 16 Sep 2004 20:25:41 +0300, Serhiy Brytskyy wrote:
Ok, but see next:

C code

double d = 29.0 * 0.01;
printf("%f\n", d);
printf("%f\n", d * 100);

result:

0.290000
29.000000

Java code:
double d = 29.0 * 0.01;
System.out.println(d);
System.out.println((d * 100));

result

0.29
28.999999999999996
So, can we come to a conclusion that Java has buggy procedures with
floating point?


No, at least not from what you've posted.

C again:

printf("%.15f\n",d);

result:
28.999999999999996

Oh wait, lets look a tiny bit closer:

printf("%.20f\n", d);
printf("%.20f\n", d * 100);
printf("%.20f\n", 0.01);

result:
0.28999999999999998002
28.999999999999996
0.01000000000000000021

So what's your point again?

Here's a clue: not all decimal numbers (e.g. 0.01) can be represented
exactly in base 2.

Here's another clue: what gets printed isn't always equal to the value
in memory.

And finally, a good source of more clues (warning: article contains
actual mathematics):

http://docs.sun.com/source/806-3568/ncg_goldberg.html

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Jul 17 '05 #4
Ok, many thanks for detailed explanation.

Serhiy

Gordon Beaton wrote:
On Thu, 16 Sep 2004 20:25:41 +0300, Serhiy Brytskyy wrote:
Ok, but see next:

C code

double d = 29.0 * 0.01;
printf("%f\n", d);
printf("%f\n", d * 100);

result:

0.290000
29.000000

Java code:
double d = 29.0 * 0.01;
System.out.println(d);
System.out.println((d * 100));

result

0.29
28.999999999999996
So, can we come to a conclusion that Java has buggy procedures with
floating point?

No, at least not from what you've posted.

C again:

printf("%.15f\n",d);

result:
28.999999999999996

Oh wait, lets look a tiny bit closer:

printf("%.20f\n", d);
printf("%.20f\n", d * 100);
printf("%.20f\n", 0.01);

result:
0.28999999999999998002
28.999999999999996
0.01000000000000000021

So what's your point again?

Here's a clue: not all decimal numbers (e.g. 0.01) can be represented
exactly in base 2.

Here's another clue: what gets printed isn't always equal to the value
in memory.

And finally, a good source of more clues (warning: article contains
actual mathematics):

http://docs.sun.com/source/806-3568/ncg_goldberg.html

/gordon

Jul 17 '05 #5

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

Similar topics

33
by: Darren Dale | last post by:
I love the language. I love the community. My only complaint is that Python for Windows is built with Visual Studio. It is too difficult to build python, or a module, from source. This is what...
6
by: Mikheil | last post by:
Hello! I need to translate file destination name with one backslashes "c:\program files\directory\file.txt" to string containing double backslashes "c:\\program files\\directory\\file.txt" If...
0
by: lmckaha | last post by:
Hi, Mysql version: 3.23.49 Solaris version: 2.7 gcc compiler version: 2.95.2 Python : 2.2.2 I'm evaluating the C and C++ API to decide which one to bye but I have many troubles.
3
by: Mr. B | last post by:
GRRRR... I've run across a situation in which I have NO solution. Hopefully there is one. VB.net. It's rather simple. I've a ComboBox that get's populated via a Database. And I pre-select...
4
by: DraguVaso | last post by:
hi, I'm having troubles putting a string with double qoutes (") in it in an application configuration file (App.Config). The string i need to put in it is: '/profile "MS Exchange Settings" ' ...
6
by: Rob.Meyer1 | last post by:
I'm working on a class for working with fractions and one of the public functions is to return the decimal value of the fraction which is simple enough except for when the denominator is 0. I...
0
by: Michal | last post by:
I have troubles with instaling .Net Framework 2.0 (Beta 2 - 2.0.50215). The main instalation went just fine, troubles begin with my attemt to run aspnet_regiss.exe -i. Asp.Net is instaled into IIS...
0
by: JohnIdol | last post by:
VC++6 to VC++2003 - linking troubles -------------------------------------------------------------------------------- Hi All, I successfully ported an application from VC++6 to VS2003. Solved...
1
by: Pegasus | last post by:
Good morning, I'm Filippo Battaglia. We're porting Apache STDCXX under Nanodesktop. We are trying to make nd compatible also with C++ and not only with C. Unfortunately, we're finding different...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...
0
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...
0
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.