The usual arithmetic operators + – * / are used in Java for addition, subtraction, multiplication, and division. The / operator denotes integer division if both arguments are integers, and floating-point division otherwise. Integer remainder (sometimes called modulus) is denoted by %. For example, 15 / 2 is 7, 15 % 2 is 1, and 15.0 / 2 is 7.5.

通常的算術運算符+ – * /在Java中用來進行加,減,乘,除。當兩個操作數都是整數時,運算符/表示整數除法,其他情況表示浮點除法。整數余數(有時也叫模數)用%表示。例如,15 / 2 等于 7, 15 % 2 等于 1, 而 15.0 / 2 等于 7.5。

Note that integer division by 0 raises an exception, whereas floating-point division by 0 yields an infinite or NaN result.

注意整數除法中0作除數將導致異常,而浮點除法中用0作除法將產生無窮或者NaN的結果。

There is a convenient shortcut for using binary arithmetic operators in an assignment. For example,

在一條賦值語句中有一個使用二元算術運算的捷徑。例如:

x += 4;

is equivalent to

等效于

x = x + 4;

(In general, place the operator to the left of the = sign, such as *= or %=.)

(總之,就是把運算符放在等號的左邊,例如*= 或者 %= 。)

NOTE注釋

?

One of the stated goals of the Java programming language is portability. A computation should yield the same results no matter which virtual machine executes it. For arithmetic computations with floating-point numbers, it is surprisingly difficult to achieve this portability. The double type uses 64 bits to store a numeric value, but some processors use 80-bit floating-point registers. These registers yield added precision in intermediate steps of a computation. For example, consider the computation:

Java編程語言的一個初衷就是可移植性。對于一個運算應該產生相同的結果,無論何種虛擬機執行該運算。對于浮點數參與的算術運算,要實現這種可移植性是驚人的困難的。雙精度類型的用64位存儲一個數值,但是一些處理器使用的是80位浮點寄存器。這些寄存器在中等級別的計算中產生額外的精度。例如,考慮如下計算:

double w = x * y / z;

Many Intel processors compute x * y and leave the result in an 80-bit register, then divide by z and finally truncate the result back to 64 bits. That can yield a more accurate result, and it can avoid exponent overflow. But the result may be different from a computation that uses 64 bits throughout. For that reason, the initial specification of the Java virtual machine mandated that all intermediate computations must be truncated. The numeric community hated it. Not only can the truncated computations cause overflow, they are actually slower than the more precise computations because the truncation operations take time. For that reason, the Java programming language was updated to recognize the conflicting demands for optimum performance and perfect reproducibility. By default, virtual machine designers are now permitted to use extended precision for intermediate computations. However, methods tagged with the strictfp keyword must use strict floating-point operations that yield reproducible results. For example, you can tag main as

許多Intel處理器計算x*y并將結果存放于80位的寄存器中,然后除以z并最終將結果省略成64位的。那可以產生更為精確的結果,并可以避免指數溢出。但是完全使用64位計算將可能產生不同的結果。由于那個原因,起初Java虛擬機規格規定所有中間運算必須被舍掉多余的精度。數字團體厭惡這種處理方式。舍去操作不僅會引起溢出,而且由于舍去操作需要占用時間而花費了比精確計算更多的時間。由此,Java編程語言進行了升級,考慮了最適宜的性能和完美的再現性這兩個互相沖突的要求。現在虛擬機設計者被許可使用擴展精度于中間級運算中。但是使用關鍵字strictfp標明的方法必須使用嚴格的浮點操作以產生有復驗性的結果,例如,你可以標記main方法如下:

public static strictfp void main(String[] args)

Then all instructions inside the main method use strict floating-point computations. If you tag a class as strictfp, then all of its methods use strict floating-point computations.

于是main方法中的所有指令都采用嚴格的浮點運算。如果你標記一個類為strictfp,那么該類的所有方法都將使用嚴格的的浮點計算。

The gory details are very much tied to the behavior of the Intel processors. In default mode, intermediate results are allowed to use an extended exponent, but not an extended mantissa. (The Intel chips support truncation of the mantissa without loss of performance.) Therefore, the only difference between default and strict mode is that strict computations may overflow when default computations don't.

詳細資料非常依賴于Intel處理器的表現。在默認模式下,中間結果被許可使用擴展指數,但不能使用擴展尾數。(Intel芯片支持不影響性能的情況下舍去尾數。)因此,默認模式和嚴格模式唯一的不同是嚴格計算可能引起溢出而默認模式不會。

If your eyes glazed over when reading this note, don't worry. Floating-point overflow isn't a problem that one encounters for most common programs. We don't use the strictfp keyword in this book.

如果閱讀此注釋時你的眼睛變得遲鈍,不要緊。浮點溢出對大多數普通編程而言并不是個能遇到的問題。在本書中我們不使用strictfp關鍵字。(汗:你既然用不著,說這么一大堆廢話干啥?害得老子翻譯了足足一個小時,還稀里糊涂的。)


文章來源:http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!304.entry