Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Problems with floating point.

Thread view: 
John - 10 Jan 2006 07:20 GMT
Hi all,

Check out the following:

public class Test
{
  public static void main(String[] args)
  {
     float x = 10.32f;
     float y = 5.81f;
     System.out.println(x - y); // Expecting 4.51; Outcome is 4.5099998
  }
}

I'm expecting 4.51 as the result of (10.32 - 5.81), but the result being
displayed is 4.5099998. Can someone please explain why this is happening,
and how would I be able to limit the answer to two decimal places.

Thanks,

John.
Richard F.L.R.Snashall - 10 Jan 2006 07:39 GMT
> Hi all,
>
[quoted text clipped - 17 lines]
>
> John.

Round off errors -- those numbers cannot be perfectly represented
inside the machine.  Standard IEEE float would give you that much
precision.
derek@gkdkwe.com - 10 Jan 2006 08:47 GMT
> > Check out the following:
> >
[quoted text clipped - 11 lines]
> > displayed is 4.5099998. Can someone please explain why this is happening,
> > and how would I be able to limit the answer to two decimal places.

(Quoted from Patricia) :

On any system, in any language, any data type based on IEEE754 64 bit
binary floating point has 53 bits, about 15.9 decimal digits, of precision.

Just as no decimal fraction can exactly represent 1/3 or 2/3,
so no binary fraction can exactly represent 1/10 or 2/10.

Subtracting numbers of equal magnitude with a matching leading decimal
digit costs a digit of precision, so it should be expected that the result will
be accurate to just under 15 decimal digits.

The unusual thing about Java in this area is the high precision of its default
String conversion for floating point values. Most languages by default round
to some reasonable number of digits.

Java converts precisely enough that if you took the toString result and parsed
it as a double you would recover the original value. This makes the rounding
error distressingly visible. To fix it, use DecimalFormat to limit the number of
digits displayed to what you actually want/need.

Example :

 double one = 23.3;
 double two = 22.2;
 double three = one - two;

 System.out.println(three);  // Output : 1.1000000000000014
 System.out.println( new DecimalFormat(".##").format(three) ); // Output : 1.1
Roedy Green - 10 Jan 2006 09:45 GMT
>I'm expecting 4.51 as the result of (10.32 - 5.81), but the result being
>displayed is 4.5099998.

see http://mindprod.com/jgloss/floatingpoint.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.