2012-04-03 23 views
6

Sto usando Math.round e sto trovando che non mi restituirà alcun valore più grande di (2^32/2) -1, ma la documentazione afferma che può/restituirà valori lunghi, cioè 2^64 ... C'è uno snippet di codice qui sotto.Math.round MAX valore restituibile

long bTmp = (long)Math.round(4294967296L); 
System.out.println(bTmp); 
System.out.println(Long.MAX_VALUE); 

quale uscita:

2147483647 
9223372036854775807 

Mi sto perdendo qualcosa?

risposta

11

Sta chiamando il sovraccarico di Math.round() che prende uno float e restituisce un int. Vedi the javadoc.

Prova:

Math.round((double) 4294967296L) 
+2

solo per aggiungere un po 'di informazioni su ciò che viene fatto: http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls- 5.1.2 Di default c'è un allargamento da long a float. Per rafforzare il comportamento con il doppio, deve essere lanciato. –