JDK-8246334 : Casting ���double to int��� and ���long to int��� produce different results
  • Type: Enhancement
  • Component: core-libs
  • Affected Version: 13
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2020-06-01
  • Updated: 2021-04-16
  • Resolved: 2021-04-16
Description
A DESCRIPTION OF THE PROBLEM :
Is there a special reason why a cast from a double bigger than Int.Max to an Integer is handled differently than a cast from a long bigger than Int.Max to an Integer? I am aware that this behaviour is described in the language specification, i.e., the resulting value will  be Int.Max for the double cast and for the long cast the additional bits will be cut off. I am just wondering about the motivation for this difference. Wouldn't it make sense to handle both casts the same way?  The following example illustrates the difference.

System.out.println(((int)2147483648L));
System.out.println(((int)2147483648.0));



Comments
There are different domains of representing numerical values. Within the domain of integer-only representations, in particular with in the domain of two's complement integer representations such as int and long in Java, the customary way to convert from a wider type (long) to a narrow one (int) is to take the low-order bits of the wider type. Across domains, such as floating-point to integer, choosing the "nearest" or "best" value is customary, leading to the observed difference. Closing as not a bug.
16-04-2021

The observation on WIndows 10 with JDK 13: jshell> System.out.println(((int)2147483648L)); -2147483648 jshell> System.out.println(((int)2147483648.0)); 2147483647
02-06-2020