Why do these two identical codes, one in C++ and the other in Java, give different results?

Asked

Viewed 143 times

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.

  • 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

1 answer

3

It is not so unusual for languages to have floating point implementations differently from each other. Of course, they are not faithfully following the IEEE specification

A possible solution is to use in Java strictfp (linking to the compilation too) and may get the same result.

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

With the issue is said to have already made the suggested, but we have not seen what. It is possible that the exercise has only been tested with C++ and cannot be done in another language. We have no way of knowing.

  • 1

    What’s more, by testing the codes I saw no difference in the output. Possibly it depends on the input. See https://ideone.com/flx87K and https://ideone.com/al6K7R.

  • Possibly..

  • I am trying to resolve this issue in URI: https://www.urionlinejudge.com.br/judge/pt/problems/view/2758 I am submitting in Java and this giving 5% error when I try to submit in C++ works perfectly. Input numbers are not provided to users (unfortunately) so we cannot know where the error is.

  • I tried EVERYTHING to resolve this issue... my last exit was to come to the stackoverflow... had already tried the strictfp, tried Bigdecimal, already tried to use other types of variables too, tried to change the way the results are printed on the screen, I’ve really tried everything but I keep getting 5% error and so I studied this difference is generated because of the accuracy of the variables. I could be wrong.

  • I’m going on a trip right now and can’t see, but when there’s a specific problem post it. I showed you what you asked. If you tried other things you should have posted everything.

  • Sorry for the inconvenience, I will edit the topic now and put more information.

  • I just edited the topic by changing the description and adding some new information hopefully it will help.

  • @Eduardomior the problem is that now you changed the question, I do not know if I want or can answer this new question, I answered the original, and new at the bottom shows another problem, not the original asked. And you put in something that we can’t verify. You say you did it, but you did something wrong. You didn’t say how you did it. It’s impossible to go wrong if you did it right, so you did it wrong, but we have no way of knowing in this question.

  • ???????? You mean that a != a being that a == a != b ... the problem at the end of the day was the java format.... the java %.f is different from the c %.f++

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.