[5400] in java-interest

home help back first fref pref prev next nref lref last post

Re: Numbers

daemon@ATHENA.MIT.EDU (Terence Chr Haddock)
Wed Feb 7 18:08:16 1996

Date: Wed, 7 Feb 1996 16:22:10 -0500 (EST)
From: Terence Chr Haddock <haddock@UDel.Edu>
To: Jill Shermer <jshermer@BASISinc.com>
cc: pat@icon-stl.net, java-interest@java.sun.com
In-Reply-To: <9602071637.AA05212@nemesis.BASISinc.com>

On Wed, 7 Feb 1996, Jill Shermer wrote:

	I am noticing this same effect. I am running Netscape 2.0b6 under 
Windows NT with the Windows NT 1.0 version of javac. When I use doubles 
that are more than 6 digits long (including digits after the decimal 
point), it appears to be rounding the last digits off to make it fit to 
six decimals. But, it sometimes appears to round up at the 3 instead of 
the 5! Here's the effects that I have seen: (I can't give you code, 
because I got this output using my spreadsheet applet, which uses doubles 
internally):

	100000.2*2 = 200000
	100000.3*2 = 200001
	100000.6*2 = 200001
	
	But now, if we take 100000.5 * 2, and divide that by 20 we get:
	
	(100000.5*2)/20=10000

	Now take, 100000.6 * 2, and divide that be 20 we get:

	(100000.6*2)/20=10000.1 !

	Why did it round up at .6 instead of .5? 

	It looks to me like toString() is truncating the doubles to six 
digits, but is using some wierd algorithm for doing so. Any comments from 
the Java team?

	- Terence C Haddock
	  haddock@udel.edu

> > Subject: Re: Numbers
> > MIME-Version: 1.0
> > X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
> > 
> > 
> > On Tue, 6 Feb 1996, Jill Shermer wrote:
> > 
> > > 2. How do you avoid loss of precision with doubles?
> > >   
> > > 	For example,  if  a = 300000.33,
> > > 			  b = 2,
> > > 		          c = a*b,
> > > 
> > > 	how do you make sure c = 600000.66,  not 600001?
> > > 	Is this avoidable?
> > 
> > It should be more clear if you declare the types of your variables. 
> > Unless you perform an explicit cast, the type of c will have to be double
> > and the value will be 600000.66
> >
> 
>  I have explicitly declared a,b, and c to be doubles in my Java code, and
>  I still lose precision. 
> 
>  -Jill
> 
> 
> -
> This message was sent to the java-interest mailing list
> Info: send 'help' to java-interest-request@java.sun.com
> 

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post