Why do these two identical codes, one in C ++ and another and Java, give different results? [closed]

0

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.

    
asked by anonymous 05.10.2018 / 06:30

1 answer

3

It is not uncommon for languages to have floating-point implementations differently from one another. Of course, they are not faithfully following the IEEE specification

One possible solution is to use Java in strictfp (linking in the build as well) and you might get the same result.

But it does not make much difference anyway because binary floating-point numbers were created to be fast and not to be exact. If you need accuracy you are using the wrong type, you probably want to use BigDecimal . See more at What is the correct way to use float, double, and decimal types? .

With the edition said that already did the suggested, but we did not see what. It is possible that the exercise has only been tested with C ++ and can not do in another language. We can not know.

    
05.10.2018 / 16:50