tiagosilva29 Posted October 2, 2004 Share Posted October 2, 2004 Hi. So we had to make this program at college that prints the sum, subtraction and multiplication of two numbers with floating point.. I compiled there with the 4.2 version... and got no issues. Now, I recompiled at home, with the new 5.0 version and I got this errors: Calculos.java:13: possible loss of precisionfound? : double required: float ? ? ? ? ? ? ? a = 3.7; ? ? ? ? ? ? ? ? ? ^ Calculos.java:14: possible loss of precision found? : double required: float ? ? ? ? ? ? ? b = 6.67; ? ? ? ? ? ? ? ? ? ^ 2 errors I don't understand what's going on. Can someone please explain w:) those errors occured? Thanks. :) The Code: /** * Programa que imprime a soma, a diferen?a e o produto de dois n?meros * de v?rgula flutuante ? minha escolha * * @author Tiago F. Silva * @version Introdu??o ? Programa??o 2004/2005 */ public class Calculos { ? ? ? public static void main(String[] arg) { ? ? ? ?float a,b; ? ? ? ?a = 3.7; ? ? ? ?b = 6.67; ? ? ? System.out.println("A soma de " + a + "+" + b + " ? " + (a+b)); ? ? ? System.out.println("A diferen?a de " + a + "-" + b + " ? " + (a-b)); ? ? ? System.out.println("O produto de " + a + "*" + b + " ? " + (a*b)); ? ? ? } } Link to comment Share on other sites More sharing options...
0 davemania Posted October 2, 2004 Share Posted October 2, 2004 try a = (float) 3.7 alternatively, you can do Float floatA = new Float(3.7) and than use floatA.floatValue .. it's something like that, i don't remeber the exact syntax Link to comment Share on other sites More sharing options...
0 turbowrx Posted October 2, 2004 Share Posted October 2, 2004 Another method to fix this problem is to place an f after the number, for example try a = 3.7f; Link to comment Share on other sites More sharing options...
0 Winston Posted October 2, 2004 Share Posted October 2, 2004 try a = (float) 3.7alternatively, you can do Float floatA = new Float(3.7) and than use floatA.floatValue .. it's something like that, i don't remeber the exact syntax Hmmm i wouldn't actually create an object just to by pass that, it's not necessary. Usually a simple casting would be enough, basically java just wants you to confirm that you know a loss of precision will occur and a casting would take care of it. Link to comment Share on other sites More sharing options...
Question
tiagosilva29
Hi.
So we had to make this program at college that prints the sum, subtraction and multiplication of two numbers with floating point..
I compiled there with the 4.2 version... and got no issues.
Now, I recompiled at home, with the new 5.0 version and I got this errors:
I don't understand what's going on.
Can someone please explain w:) those errors occured? Thanks. :)
The Code:
/** * Programa que imprime a soma, a diferen?a e o produto de dois n?meros * de v?rgula flutuante ? minha escolha * * @author Tiago F. Silva * @version Introdu??o ? Programa??o 2004/2005 */ public class Calculos { ? ? ? public static void main(String[] arg) { ? ? ? ?float a,b; ? ? ? ?a = 3.7; ? ? ? ?b = 6.67; ? ? ? System.out.println("A soma de " + a + "+" + b + " ? " + (a+b)); ? ? ? System.out.println("A diferen?a de " + a + "-" + b + " ? " + (a-b)); ? ? ? System.out.println("O produto de " + a + "*" + b + " ? " + (a*b)); ? ? ? } }Link to comment
Share on other sites
3 answers to this question
Recommended Posts