Home arrow static arrow Java Programming [Archive] - floating point
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - floating point
This topic has 37 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:236
Registered: 5/11/04
floating point  
Jul 29, 2004 12:30 AM



 
i understand that floating point numbers are represented in java at binary numbers..
and that there are decimal values that cannot exactly representable in binary..

one of that i think is 0.1

but why is it that 0.1 can be represented exactly but in the computation it cannot.

i have this program..

public class CTest{    public static void main(String[] args)    {          double d = 1.0;          double d2 = 0.9;          double d3 = 0.1;          System.out.println(""+(d-d2));          System.out.println(""+d3);           }}


please help..
thanks
 

Posts:392
Registered: 3/7/03
Re: floating point  
Jul 29, 2004 12:48 AM (reply 1 of 37)



 
http://java.sun.com/developer/JDCTechTips/2003/tt0204.html
check out this link

The suggestions:
There are several ways to fix the equality testing problem illustrated above.

1. you could use the BigDecimal class, and opt out of floating-point arithmetic entirely.
2. check that resulting value is close in value rather than exactly equal to the desired one.

hope this helps
 

Posts:743
Registered: 7/12/01
Re: floating point  
Jul 29, 2004 12:56 AM (reply 2 of 37)



 
A floating point number uses internally the scientific representation 3.1425x10^2 = 314.25.
A floating point number does this as follows:

Ensure the before the decimal point is a 0, so: 0.31425x10^1, giving mantisse .31425 and exponent 1.
From the 64 bits a fixed number are used for mantisse and exponent.

For huge numbers the gap between two bit differing numbers will become ever larger.

For small numbers the mantisse is a binary fraction, not a decimal one, 0 or 1 * 2^(-n)
Now 1/10 = 0.0001 (1/16) + ...
This is not a finite sequence, so can only be an approximation.

But 0.25 = 0.01
 

Posts:236
Registered: 5/11/04
Re: floating point  
Jul 29, 2004 1:00 AM (reply 3 of 37)



 
i understand that 0.1 just like other values cannot be represented exactly in binary..

that:

double d = 0.1 + 0.1 + 0.1;

is not exactly 0.3.

why, when you assign 0.1 directly to a double variabl as in:
double d = 0.1;

when you use System.out.prinln, it can be displayed as 0.1

but when after an expression..it is not exactly 0.1..

why is this so?

is the System.out.prinln() changing it?

 

Posts:2,830
Registered: 9/1/03
Re: floating point  
Jul 29, 2004 1:10 AM (reply 4 of 37)



 
is the System.out.prinln() changing it?

What do you mean ? This program prints "0.1" twice for me:
class D {	public static void main(String[] args){		double d = 0.1;		System.out.println("d: " + d);		System.out.println("d: " + d);	}}
 

Posts:236
Registered: 5/11/04
Re: floating point  
Jul 29, 2004 1:16 AM (reply 5 of 37)



 
I mean this one..

[code]
class D {
public static void main(String[] args){
double d = 0.1;
double d2 = 1.0 - 0.9;
System.out.println("d: " + d);
System.out.println("d: " + d2);
}
}
[\code]
 

Posts:2,830
Registered: 9/1/03
Re: floating point  
Jul 29, 2004 1:29 AM (reply 6 of 37)



 
yes, that one is due to the way java implements floating point math, i think
a previous link in this thread might explain it.
 

Posts:2,830
Registered: 9/1/03
Re: floating point  
Jul 29, 2004 1:30 AM (reply 7 of 37)



 
if it doesn't the site: http://thejword.com has a link to a nice discussion about floating point.
 

Posts:236
Registered: 5/11/04
Re: floating point  
Jul 29, 2004 1:33 AM (reply 8 of 37)



 
i think this was discussed so many times before, but i could not find the exact link..

by the way, how many places is the default decimal places that can be displayed by System.out.println?

thanks
 

Posts:5,965
Registered: 5/17/03
Re: floating point  
Jul 29, 2004 1:37 AM (reply 9 of 37)



 
is the System.out.prinln() changing it?

In a way it does. It takes the internal binary floating point representation and produces the closes decimal equivalent. 1.0 and 0.9 both show up just as expected but 1.0-0.9 doesn't. Because of the subtraction operation 1.0-0.9 doesn't get the same floating point representation as 0.1.

So mathematically 1.0-0.9 and 0.1 are the same but numerically in Java they're not.
 

Posts:5,965
Registered: 5/17/03
Re: floating point  
Jul 29, 2004 1:46 AM (reply 10 of 37)



 
Also these small "representation" errors can add in numerical calculations and become a problem, like

double d = 1.0-0.1-0.1-0.1-0.1-0.1-0.1-0.1-0.1-0.1;
System.out.println(d);

Mathematically d should be 0.1 but numerically it definately isn't.
 

Posts:236
Registered: 5/11/04
Re: floating point  
Jul 29, 2004 1:56 AM (reply 11 of 37)



 
how does java implementation making to appear in the screen as 0.1 for direct assignment?

I think System.out.println does it but I could not find the default number of decimal places it used.

could you please share how to do it..
say I want to display 0.1 upto 20 decimal places using System.out.println..

thanks
 

Posts:236
Registered: 5/11/04
Re: floating point  
Jul 29, 2004 4:44 AM (reply 12 of 37)



 
anybody please...

why is it that..

double d1 = 0.1;

prints 0.1 when used in System.out.println(d1);

while d1 = 1.0 - 0.9;

does not print 0.1 exactly?

the latter probably due to the floating point specs, but why is it that when directly assigning to
double d1 = 0.1, it is 0.1 and not 0.09999999999999998?

thank you.
 

Posts:24,036
Registered: 2/3/03
Re: floating point  
Jul 29, 2004 6:13 AM (reply 13 of 37)



 
the latter probably due to the floating point specs,
but why is it that when directly assigning to
double d1 = 0.1, it is 0.1 and not
0.09999999999999998?

[url=http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2]SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC[/url]
[url=http://docs.sun.com/source/806-3568/ncg_goldberg.html]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]
 

Posts:5,965
Registered: 5/17/03
Re: floating point  
Jul 29, 2004 7:15 AM (reply 14 of 37)



 
anybody please...

why is it that..

Don't you read the posts? I've already told you that 0.1 and 1.0-0.9 are mathematically but not numerically equivalent. In the 1.0-0.9 case the subtraction operation produces a "representation error" which is bigger than in the "pure" 0.1 case. This "error" is large enought to not disappear when the internal binary representation is turned into decimal by println. I've also shown you how these "errors" can be accumulative and add up to substantial amounts.
 
This topic has 37 replies on 3 pages.    1 | 2 | 3 | Next »