最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java求余的技巧匯總

 更新時間:2019年09月25日 08:31:21   作者:一天不進步,就是退步  
這篇文章主要給大家介紹了關于java求余技巧的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

背景

傳說里玉皇大帝派龍王馬上降雨到共光一帶,龍王接到玉皇大帝命令,立馬從海上調水,跑去共光施云布雨,但粗心又著急的龍王不小心把海里的鯨魚隨著雨水一起降落在了共光,龍王怕玉皇大帝責怪,靈機一動便聲稱他是派魚到共光,希望百姓可以年年有余,并請求玉皇大帝將這條魚任命為魚神,保佑人間太平可以年年有余。

年年有余

java 求余操作初階

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

例1:

int a = 5%3; // 2
int b = 5/3; // 1
System.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規(guī)范告訴我們:

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

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

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);
 }
}

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

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

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

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 division, not a truncating division, 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 dividend.
If the dividend is an infinity, or the divisor is a zero, or both, the result is NaN.
If the dividend is finite and the divisor is an infinity, the result equals the dividend.
If the dividend is a zero and the divisor is finite, the result equals the dividend.
In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from the division of a dividend n by a divisor 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 求余操作骨灰級

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

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: -2
at 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里有很多小技巧,需要我們勤翻api和jsl,多學習多練習。

參考資料:

【1】https://baike.baidu.com/item/%E5%B9%B4%E5%B9%B4%E6%9C%89%E4%BD%99/7625174?fr=aladdin

【2】https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.17.3

【3】java解惑

總結

以上就是我在處理客戶端真實IP的方法,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:

相關文章

  • MyBatis-Plus中如何配置加密功能(使用AES算法)

    MyBatis-Plus中如何配置加密功能(使用AES算法)

    本文將詳細介紹如何實現 MyBatis-Plus 中的配置加密功能,并給出相應的代碼示例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java如何基于ProcessBuilder類調用外部程序

    Java如何基于ProcessBuilder類調用外部程序

    這篇文章主要介紹了Java如何基于ProcessBuilder類調用外部程序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • 用Java驗證pdf文件的電子章簽名

    用Java驗證pdf文件的電子章簽名

    這篇文章主要介紹了如何用Java驗證pdf文件的電子章簽名,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-12-12
  • Java基礎之線程鎖相關知識總結

    Java基礎之線程鎖相關知識總結

    今天給大家?guī)淼氖顷P于Java線程的相關知識,文章圍繞著Java線程鎖展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • spring security在分布式項目下的配置方法(案例詳解)

    spring security在分布式項目下的配置方法(案例詳解)

    這篇文章主要介紹了spring security在分布式項目下的配置方法,本文通過一個項目案例給大家詳細介紹,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • maven打包成第三方jar包且把pom依賴包打入進來的方法

    maven打包成第三方jar包且把pom依賴包打入進來的方法

    這篇文章主要介紹了maven打包成第三方jar包且把pom依賴包打入進來的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 全面解析java final關鍵字

    全面解析java final關鍵字

    這篇文章主要介紹了java final關鍵字的使用,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-01-01
  • RabbitMQ 避免消息重復消費的方法

    RabbitMQ 避免消息重復消費的方法

    消費者端實現冪等性,意味著消息永遠不會消費多次,即使收到了多條一樣的消息,這篇文章給大家分享RabbitMQ 避免消息重復消費的方法,感興趣的朋友一起看看吧
    2024-03-03
  • Spring Boot XSS 攻擊過濾插件使用

    Spring Boot XSS 攻擊過濾插件使用

    這篇文章主要介紹了Spring Boot XSS 攻擊過濾插件使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • mybatis.type-aliases-package的作用及用法說明

    mybatis.type-aliases-package的作用及用法說明

    這篇文章主要介紹了mybatis.type-aliases-package的作用及用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

阿图什市| 蒲城县| 邵阳市| 宁武县| 金山区| 涟源市| 禹城市| 塘沽区| 昭通市| 永福县| 彰武县| 泸西县| 水城县| 罗定市| 灌南县| 安平县| 长丰县| 阿拉善盟| 全州县| 临西县| 铁岭县| 郸城县| 双柏县| 丰宁| 富阳市| 清水县| 金华市| 陆川县| 凤庆县| 六盘水市| 花莲县| 连云港市| 岳阳市| 许昌市| 池州市| 来凤县| 吉林省| 西城区| 留坝县| 潮州市| 葵青区|