I'm trying to resolve an issue in the Online Judge URI in Java however I'm remarking 5 % error, and from what I've studied is being caused by the fact that the precision of the variables in C ++ and Java is different. I've tried everything to try to get Accept in Java but I could not ... I "converted" the Java code to C ++ and it worked perfectly.
I already tried to use strictfp
, I already tried to use BigDecimal
, I already tried to change the precision of BigDecimal
, tried to change the variables, tried to change the methods of print , tried to use other modifiers but I did not get it at all, then everything leads one to believe that it is the fault of the precision of the floating point values.
Java Code:
Scanner leitor = new Scanner(System.in);
float a = leitor.nextFloat();
float b = leitor.nextFloat();
double c = leitor.nextDouble();
double d = leitor.nextDouble();
System.out.printf("A = %f, B = %f\n",a,b);
System.out.printf("C = %f, D = %f\n",c,d);
System.out.printf("A = %.1f, B = %.1f\n",a,b);
System.out.printf("C = %.1f, D = %.1f\n",c,d);
System.out.printf("A = %.2f, B = %.2f\n",a,b);
System.out.printf("C = %.2f, D = %.2f\n",c,d);
System.out.printf("A = %.3f, B = %.3f\n",a,b);
System.out.printf("C = %.3f, D = %.3f\n",c,d);
System.out.printf("A = %.3E, B = %.3E\n",a,b);
System.out.printf("C = %.3E, D = %.3E\n",c,d);
System.out.printf("A = %.0f, B = %.0f\n",a,b);
System.out.printf("C = %.0f, D = %.0f\n",c,d);
C ++ code:
float a, b;
double c, d;
cin >> a;
cin >> b;
cin >> c;
cin >> d;
printf("A = %f, B = %f\n",a,b);
printf("C = %f, D = %f\n",c,d);
printf("A = %.1f, B = %.1f\n",a,b);
printf("C = %.1f, D = %.1f\n",c,d);
printf("A = %.2f, B = %.2f\n",a,b);
printf("C = %.2f, D = %.2f\n",c,d);
printf("A = %.3f, B = %.3f\n",a,b);
printf("C = %.3f, D = %.3f\n",c,d);
printf("A = %.3E, B = %.3E\n",a,b);
printf("C = %.3E, D = %.3E\n",c,d);
printf("A = %.0f, B = %.0f\n",a,b);
printf("C = %.0f, D = %.0f\n",c,d);
The results in C ++ are coming out 5% different from the results in Java. Unfortunately I do not have access to results I only have access to the site's response.