Cannot believe this. Problems with double and float

  • Thread starter Giorgio Franceschetti
  • Start date
G

Giorgio Franceschetti

Well, I coudn't really believe this, but....
this is what happened:
I have made a simple program adding three simple floating point values.
The program is like that:
/*
* testDouble.java
*
* Created on 16 gennaio 2004, 15.09
*/

package test;

/**
*
* @author gfranceschetti
*/
public class testDouble {

/** Creates a new instance of testDouble */
public testDouble() {
double sum = 0;
sum += 1.1;
System.out.println(sum);
sum += 1.2;
System.out.println(sum);
sum += 1.4;
System.out.println(sum);
sum -= 0.1;
System.out.println(sum);
double test = (1.1 + 1.2 + 1.3);
System.out.println(test);

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new testDouble();
}

}

The result is as follows:
1.1
2.3
3.699999999999997
3.599999999999996
3.599999999999996

After that I modified th program adding similiar instructions managing
floats. This is the program:
/*
* testDouble.java
*
* Created on 16 gennaio 2004, 15.09
*/

package test;

/**
*
* @author gfranceschetti
*/
public class testDouble {

/** Creates a new instance of testDouble */
public testDouble() {
double sum = 0;
sum += 1.1;
System.out.println(sum);
sum += 1.2;
System.out.println(sum);
sum += 1.4;
System.out.println(sum);
sum -= 0.1;
System.out.println(sum);
double test = (1.1 + 1.2 + 1.3);
System.out.println(test);
float sum1 = 0;
sum1 += 1.1;
System.out.println(sum1);
sum1 += 1.2;
System.out.println(sum1);
sum1 += 1.4;
System.out.println(sum1);
sum1 -= 0.1;
System.out.println(sum1);

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new testDouble();
}

}

And the following is the result:
1.1
2.3
3.699999999999997
3.599999999999996
3.599999999999996
1.1
2.3
3.7
3.6000001 !!!!!!!!!!!!!!

After that I added two more instructions as follows:
float test1 = (1.1 + 1.2 + 1.3);
System.out.println(test1);

But now the program doesn't even compile!!!!!!!
the error message is as follows:
testDouble.java possible loss of precision.

Please tell me that I'm making some strange error....

Thanks in advance,
Giorgio
 
W

W.

Giorgio said:
Well, I coudn't really believe this, but....
this is what happened:
I have made a simple program adding three simple floating point values.
The program is like that:
/*
* testDouble.java
*
* Created on 16 gennaio 2004, 15.09
*/

package test;

/**
*
* @author gfranceschetti
*/
public class testDouble {

/** Creates a new instance of testDouble */
public testDouble() {
double sum = 0;
sum += 1.1;
System.out.println(sum);
sum += 1.2;
System.out.println(sum);
sum += 1.4;
System.out.println(sum);
sum -= 0.1;
System.out.println(sum);
double test = (1.1 + 1.2 + 1.3);
System.out.println(test);

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new testDouble();
}

}

The result is as follows:
1.1
2.3
3.699999999999997
3.599999999999996
3.599999999999996

After that I modified th program adding similiar instructions managing
floats. This is the program:
/*
* testDouble.java
*
* Created on 16 gennaio 2004, 15.09
*/

package test;

/**
*
* @author gfranceschetti
*/
public class testDouble {

/** Creates a new instance of testDouble */
public testDouble() {
double sum = 0;
sum += 1.1;
System.out.println(sum);
sum += 1.2;
System.out.println(sum);
sum += 1.4;
System.out.println(sum);
sum -= 0.1;
System.out.println(sum);
double test = (1.1 + 1.2 + 1.3);
System.out.println(test);
float sum1 = 0;
sum1 += 1.1;
System.out.println(sum1);
sum1 += 1.2;
System.out.println(sum1);
sum1 += 1.4;
System.out.println(sum1);
sum1 -= 0.1;
System.out.println(sum1);

}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new testDouble();
}

}

And the following is the result:
1.1
2.3
3.699999999999997
3.599999999999996
3.599999999999996
1.1
2.3
3.7
3.6000001 !!!!!!!!!!!!!!

After that I added two more instructions as follows:
float test1 = (1.1 + 1.2 + 1.3);
System.out.println(test1);

But now the program doesn't even compile!!!!!!!
the error message is as follows:
testDouble.java possible loss of precision.

Please tell me that I'm making some strange error....

Thanks in advance,
Giorgio

Expression (1.1 +1.2 1.3) is evaluated in double then you
assign the result to float so there is a possible loss of
precision. You must make an explicite cast from double to float

float test1 = (float)(1.1 + 1.2 + 1.3);

W.
 
J

Jon Skeet

Giorgio Franceschetti said:
After that I added two more instructions as follows:
float test1 = (1.1 + 1.2 + 1.3);
System.out.println(test1);

But now the program doesn't even compile!!!!!!!
the error message is as follows:
testDouble.java possible loss of precision.

Indeed - literal floating point values are doubles by default, so the
result of the expression is a double, but you're trying to assign it to
a float.
Please tell me that I'm making some strange error....

Yup - you're misunderstanding how floating point works.

See http://www.pobox.com/~skeet/csharp/floatingpoint.html for more
information. It's .NET based, but the basics apply to Java as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top