• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

double (exponent) trouble

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have assigned a value to double variable:

double d = 22222222222222222222.00; // [twenty 2s]

[yes, I did check whether d could hold a value as big as this, using (d < Double.MAX_VALUE), which returned true]

Now when I print the same using System.out.println(d), I get the following out put:

2.222222222222222E19

How do I print the value as I entered it? Also in what form has the number been converted? Exponential? Then what exponent and base? [http://www.webwinder.com/wwhtmbin/jexpont.html]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that you need to read-up on floating-point arithmetic. The types "float" and "double" are floating-point.

Floating-point types can store values with a very wide range of magnitude, but they cannot store all values precisely. In your case, you are hoping for twenty decimal digits of precision, but even a "double" can only store about 14 (something like that, anyway).

When performing calculations with floating-point, errors accumulate, due to the imprecision of the values. This means that you can rarely trust the last digit of the value. For complex calculations, further digits may become untrustworthy, too.

Typically, when printing-out a "float" or "double", you should use NumberFormat, to format the value to a number of decimal digits that suits your application and is within the accuracy of the calculations performed.

Note that issues of floating-point precision are common to most languages and also applications like spreadsheets. It's not specific to Java. In my opinion, this is far from an "advanced" topic, by the way; it's something that anyone doing calculations with a computer should know.
[ February 24, 2005: Message edited by: Peter Chase ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need exact decimal numbers (both integer and floating point), you have BigInteger and BigDecimal. You pay a small performance cost but gain precision.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you're losing precision here. Using a Java 1.5 Formatter gives the following...

To help understand why, see this post:
https://coderanch.com/t/375505/java/java/Java-Subtraction-Incorrect
[ February 24, 2005: Message edited by: marc weber ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not advanced. Moving to JiG(I) .
 
reply
    Bookmark Topic Watch Topic
  • New Topic