2

Why on php this printf((2376995291 - 141 * 16777216) / 65535) on

result 174.0724040589.

On Java System.out.println((2376995291L - 141 * 16777216) / 65535)

result 65711 .

Why its two different result and how get result PHP on Java , i need get 174 on java.

1
  • 2
    141 * 16777216 is too big for an int. 141 * 16777216L will do the calculation as a long instead. Commented Jun 26, 2019 at 11:10

5 Answers 5

4
System.out.println((2376995291L - 141L * 16777216L) / (double)65535);

141 * 16777216 will technically overflow since it's greater than 2^31 - 1. So, make them as long numbers to avoid overflow and you could typecast the denominator if you like to get the result with decimal points as well.

Sign up to request clarification or add additional context in comments.

Comments

1

In java Try this

System.out.println((2376995291L - 141 * 16777216L) / 65535L);

Comments

0

You need to cast the number to float or Java will simply ignore the decimal places.

System.out.println(((float) (2376995291L - 141L * 16777216L)) / 65535f)

2 Comments

It still giving 65711.07 while in calc its giving 174
@Mak You also need to tell Java that the multiplication will produce a Long or it will overflow and give you a wrong number. Works now.
0

Because multiplication of is long number.Need to define it as long

Splited into 2 step for just clarification.

    public static void main(String[] args) {
            Long te = (141L * 16777216L);
            System.out.println((2376995291L - te) / 65535);

        }

Comments

0

141 * 16777216 is too big for an int. If you do that part of the calculation as a long, you'll get the right answer.

jshell> (2376995291L - 141 * 16777216L) / 65535
==> 174

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.