Reputation: 97
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.
Upvotes: 2
Views: 81
Reputation: 464
In java Try this
System.out.println((2376995291L - 141 * 16777216L) / 65535L);
Upvotes: 1
Reputation: 59146
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
Upvotes: 0
Reputation: 1078
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);
}
Upvotes: 0
Reputation: 17805
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.
Upvotes: 4
Reputation: 1580
You need to cast the number to float or Java will simply ignore the decimal places.
System.out.println(((float) (2376995291L - 141L * 16777216L)) / 65535f)
Upvotes: 0