91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java求余的操作介紹

發布時間:2021-09-14 23:08:28 來源:億速云 閱讀:178 作者:chen 欄目:編程語言

本篇內容主要講解“java求余的操作介紹”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java求余的操作介紹”吧!

java 求余操作初階

java中也有余的規范【jls-15.17.3】,廢話不說,直接上代碼,從中我們可以學到很多技巧:

例1:

int a = 5%3; // 2int b = 5/3; // 1System.out.println("5%3 produces " + a +" (note that 5/3 produces " + b + ")");

相信大多數人都知道結果了:

5%3 produces 2 (note that 5/3 produces 1)

java 求余操作中階

我們知道,正數不僅僅有正整數還有負整數,那么負數的情況下,會出現什么變化呢?

例2:

int c = 5%(-3); // 2    int d = 5/(-3); // -1    System.out.println("5%(-3) produces " + c +" (note that 5/(-3) produces " + d + ")");    int e = (-5)%3; // -2    int f = (-5)/3; // -1    System.out.println("(-5)%3 produces " + e +" (note that (-5)/3 produces " + f + ")");    int g = (-5)%(-3); // -2    int h = (-5)/(-3); // 1    System.out.println("(-5)%(-3) produces " + g +" (note that (-5)/(-3) produces " + h + ")");

能完全正確得到結果的就很少了吧?

          5%(-3) produces 2 (note that 5/(-3) produces -1)          (-5)%3 produces -2 (note that (-5)/3 produces -1)          (-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)

為什么求余的結果是這樣的呢?jls-15.17.3規范告訴我們:

The binary % operator is said to yield the remainder of its operands from an implied pision; the left-hand operand is the pidend and the right-hand operand is the pisor.It follows from this rule that the result of the remainder operation can be negative only if the pidend is negative, and can be positive only if the pidend is positive. Moreover, the magnitude of the result is always less than the magnitude of the pisor.

注意:求余的正負數給pidend(左邊操作數)的符號位一致!

java 求余操作高階

java求余操作不但支持整數還支持浮點數

class Test2 { public static void main(String[] args) { double a = 5.0%3.0; // 2.0 System.out.println("5.0%3.0 produces " + a); double b = 5.0%(-3.0); // 2.0 System.out.println("5.0%(-3.0) produces " + b); double c = (-5.0)%3.0; // -2.0 System.out.println("(-5.0)%3.0 produces " + c); double d = (-5.0)%(-3.0); // -2.0 System.out.println("(-5.0)%(-3.0) produces " + d); }}

相信很多人可以根據整型的規則,得出正確的結果

5.0%3.0 produces 2.05.0%(-3.0) produces 2.0(-5.0)%3.0 produces -2.0(-5.0)%(-3.0) produces -2.0

補充一下,浮點型的求余有一些特殊的規則:

The result of a floating-point remainder operation as computed by the % operator is not the same as that produced by the remainder operation defined by IEEE 754. The IEEE 754 remainder operation computes the remainder from a rounding pision, not a truncating pision, and so its behavior is not analogous to that of the usual integer remainder operator. Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. The IEEE 754 remainder operation may be computed by the library routine Math.IEEEremainder.

The result of a floating-point remainder operation is determined by the rules of IEEE 754 arithmetic:

If either operand is NaN, the result is NaN.If the result is not NaN, the sign of the result equals the sign of the pidend.If the pidend is an infinity, or the pisor is a zero, or both, the result is NaN.If the pidend is finite and the pisor is an infinity, the result equals the pidend.If the pidend is a zero and the pisor is finite, the result equals the pidend.In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from the pision of a pidend n by a pisor d is defined by the mathematical relation r = n - (d ? q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is as large as possible without exceeding the magnitude of the true mathematical quotient of n and d.Evaluation of a floating-point remainder operator % never throws a run-time exception, even if the right-hand operand is zero. Overflow, underflow, or loss of precision cannot occur.

java 求余操作骨灰級

學到這里,或許有人沾沾自喜,我都掌握了求余的所有規則,看來需要給你潑潑冷水:

public static void main(String[] args) {    final int MODULUS = 3;    int[] histogram = new int[MODULUS];    // Iterate over all ints (Idiom from Puzzle 26)    int i = Integer.MIN_VALUE;    do {    histogram[Math.abs(i) % MODULUS]++;    } while (i++ != Integer.MAX_VALUE);    for (int j = 0; j < MODULUS; j++)    System.out.println(histogram[j] + " ");  }

這個程序會打印什么?有人經過繁瑣復雜的算出一個結果:

1431655765 1431655766 1431655765

但其實,上述程序運行報錯:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2at com.java.puzzlers.ModTest.main(ModTest.java:11)

為什么數組會出現索引 -2?奇怪吧?要回答這個問題,我們必須要去看看Math.abs 的文檔

/** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * * <p>Note that if the argument is equal to the value of * {@link Integer#MIN_VALUE}, the most negative representable * {@code int} value, the result is that same value, which is * negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static int abs(int a) { return (a < 0) ? -a : a; }

特意說明,如果是Integer#MIN_VALUE,返回負數

到此,相信大家對“java求余的操作介紹”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

乌拉特中旗| 高要市| 星座| 芜湖县| 从化市| 台湾省| 密云县| 亚东县| 高要市| 辽宁省| 自治县| 利川市| 嘉黎县| 张家口市| 余干县| 海淀区| 岳阳县| 札达县| 玛曲县| 廊坊市| 珲春市| 米易县| 双峰县| 海阳市| 英德市| 丽江市| 安乡县| 安达市| 大港区| 富顺县| 大冶市| 繁峙县| 丰宁| 正阳县| 永和县| 双峰县| 营口市| 高陵县| 阿城市| 六盘水市| 肥乡县|