Diff between (a+b)/2 and a+(b-a)/2

int a = 1000000000;
int b = 2000000000;
print((a+b)/2);
print(a+(b-a)/2);
Output:
-647483648
1500000000
int a = 2000000000;
int b = 1000000000;
print(a+b);
print((a+b)/2);
print(b-a);
print((b-a)/2);
print(a+(b-a)/2);
Output:
-1294967296
-647483648
-1000000000
-500000000
1500000000

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store