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
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