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.

通常的算術(shù)運(yùn)算符+ – * /在Java中用來(lái)進(jìn)行加,減,乘,除。當(dāng)兩個(gè)操作數(shù)都是整數(shù)時(shí),運(yùn)算符/表示整數(shù)除法,其他情況表示浮點(diǎn)除法。整數(shù)余數(shù)(有時(shí)也叫模數(shù))用%表示。例如,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.

注意整數(shù)除法中0作除數(shù)將導(dǎo)致異常,而浮點(diǎn)除法中用0作除法將產(chǎn)生無(wú)窮或者NaN的結(jié)果。

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

在一條賦值語(yǔ)句中有一個(gè)使用二元算術(shù)運(yùn)算的捷徑。例如:

x += 4;

is equivalent to

等效于

x = x + 4;

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

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

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編程語(yǔ)言的一個(gè)初衷就是可移植性。對(duì)于一個(gè)運(yùn)算應(yīng)該產(chǎn)生相同的結(jié)果,無(wú)論何種虛擬機(jī)執(zhí)行該運(yùn)算。對(duì)于浮點(diǎn)數(shù)參與的算術(shù)運(yùn)算,要實(shí)現(xiàn)這種可移植性是驚人的困難的。雙精度類型的用64位存儲(chǔ)一個(gè)數(shù)值,但是一些處理器使用的是80位浮點(diǎn)寄存器。這些寄存器在中等級(jí)別的計(jì)算中產(chǎn)生額外的精度。例如,考慮如下計(jì)算:

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處理器計(jì)算x*y并將結(jié)果存放于80位的寄存器中,然后除以z并最終將結(jié)果省略成64位的。那可以產(chǎn)生更為精確的結(jié)果,并可以避免指數(shù)溢出。但是完全使用64位計(jì)算將可能產(chǎn)生不同的結(jié)果。由于那個(gè)原因,起初Java虛擬機(jī)規(guī)格規(guī)定所有中間運(yùn)算必須被舍掉多余的精度。數(shù)字團(tuán)體厭惡這種處理方式。舍去操作不僅會(huì)引起溢出,而且由于舍去操作需要占用時(shí)間而花費(fèi)了比精確計(jì)算更多的時(shí)間。由此,Java編程語(yǔ)言進(jìn)行了升級(jí),考慮了最適宜的性能和完美的再現(xiàn)性這兩個(gè)互相沖突的要求。現(xiàn)在虛擬機(jī)設(shè)計(jì)者被許可使用擴(kuò)展精度于中間級(jí)運(yùn)算中。但是使用關(guān)鍵字strictfp標(biāo)明的方法必須使用嚴(yán)格的浮點(diǎn)操作以產(chǎn)生有復(fù)驗(yàn)性的結(jié)果,例如,你可以標(biāo)記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方法中的所有指令都采用嚴(yán)格的浮點(diǎn)運(yùn)算。如果你標(biāo)記一個(gè)類為strictfp,那么該類的所有方法都將使用嚴(yán)格的的浮點(diǎn)計(jì)算。

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.

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

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.

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


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