float과 long 타입의 implicit casting
Posted: April 27, 2014 Filed under: Code, Tajo | Tags: Tajo Leave a comment4 byte 짜리 float이 8 byte 짜리 long보다 widen 더 넓은 범위를 표현하는 type 이었다. 지금까지 Tajo에서 float 과 long의 산술 연산에서 둘다 double로 casting 하고 처리했었는데 사실은 long을 float으로 casting 한 후 해야 IEEE 754에 기반한 결과가 나오는 것이었음.
- http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2
- http://stackoverflow.com/questions/1293819/why-does-java-implicitly-without-cast-convert-a-long-to-a-float
- http://stackoverflow.com/questions/5563000/implicit-type-conversion-rules-in-c-operators
(4월 28일 Updated ) precision은 당연히 64bit인 long이 더 높다. 하지만 수의 표현 범위만 봤을 때는 float이 더 넓다. 따라서 float과 long의 산술 연산은 long이 float으로 implicit conversion 되어진 후 계산된다.
- float.MaxValue ~= 3.402823+e38
- long.MaxValue ~= 9.223372+e18
(4월 28일 Update 2) 정밀도(precision)의 차이가 낮기 때문에 int, long에서 float 또는 long에서 double로 변환될 때 값이 손실 될 수 가 있다. 그러나 항상은 아님.
A widening conversion of an
int
or along
value tofloat
, or of along
value todouble
, may result in loss of precision – that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).