0
I’m trying to solve an issue in the URI Online Judge in Java but I’m afraid of 5% error, and what I’ve studied is being caused by the fact that the accuracy of variables in C++ and Java is different. I’ve tried everything to try to get Accept in Java but I couldn’t... I "converted" the Java code to C++ and it worked perfectly.
I’ve tried using the strictfp
, I’ve tried using BigDecimal
, I’ve tried to change the accuracy of BigDecimal
, tried to change the variables, tried to change the methods of print, I tried to use other modifiers but I couldn’t get anything, so everything leads me to believe that the fault lies with 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);
C++ results are 5% different from Java results. Unfortunately I do not have access to the results only have access to the website response.
What values are you testing with ? @bfavaretto showed an example in Ideone where the output is exactly the same.
– Isac
Unfortunately I don’t possess the input values. I am trying to resolve this issue of URI https://www.urionlinejudge.com.br/judge/pt/problems/view/2758
– Eduardo Mior