Java 取余 (remain),取模 (mod) 的 区别和运算

范围区别:取模主要是用于计算机术语中。取余则更多是数学概念。
主要的区别在于对负整数进行除法运算时操作不同

那么具体是怎样的不同?
首先需要知道Java中如何取模:

  • Java中用符号%对数字进行取模,可以得到以下:

    • System.out.println(5%3);
      System.out.println(-5%3);
      System.out.println(5%-3);
      System.out.println(-5%-3);
      
  • 运用Math类方法对其进行取模:

    •  System.out.println(Math.floorMod(5,3));
       System.out.println(Math.floorMod(-5,3));
       System.out.println(Math.floorMod(5,-3));      	
       System.out.println(Math.floorMod(-5,-3)); 
      
  • 输出结果

输出结果

  • 上述结果证明,两个取模运算并不相同,那么 % 和 Math.floorMod 中的运算到底哪个是取模?哪个是取余?(可能根据类你已经知道结果了)

  • 那么我们继续根据BigInteger封装的mod方法,remain方法进行比较:

         BigInteger a = new BigInteger(String.valueOf(-5));
         BigInteger mod = a.mod(BigInteger.valueOf(3));
         System.out.println(mod);    				//BigInteger.mod()取模,结果为1
         System.out.println(Math.floorMod(-5,3));    //Math取模,结果为1
         System.out.println(-5%3);   				//%结果为-2
         BigInteger remainder = a.remainder(BigInteger.valueOf(3));
         System.out.println(remainder);				//BigInteger.remain()取余结果为-2
    
  • 由此可以得出一个结论:%的取模,不是实际意义上的取模,底层是取余操作。
    (此处应该注意,有的同学在使用BigInteger的mod方法时,传负参数,会导致ArithmeticException的错误,public BigInteger mod(BigInteger m) 如果 m ≤ 0会导致ArithmeticException )

取余,遵循尽可能让商大的原则
-----取余:尽可能让商的绝对值更小
-----如-9rem2,-9/2=-4.5,取商为-4,。余数为-9-(2*-4)=-1
取模,遵循尽可能让商小的原则
-----取模:“向下”的意思就是说尽可能让商更小
-----如-9mod2,-9/2=-4.5,取商为-5。余数为-9-(2*-5)=1

为了方便记忆:

  • a ÷ b = c ··· r
    a,b 同号 时(a,b>0 || a,b<0),取模取余相同都为r
    当a,b 异号时,模为 (r + b) ,余为 r

注意:在C/C++, C#, JAVA, PHP这几门主流语言中,’%’运算符都是做取余运算,而在
python中的’%’是做取模运算。

参考文献:https://blog.csdn.net/coder_panyy/article/details/73743722
https://blog.csdn.net/songsong2017/article/details/84033883

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Get a grounding in polymorphism and other fundamental aspects of object-oriented program design and implementation, and learn a subset of design patterns that any practicing Java professional simply must know in today’s job climate. Java Program Design presents program design principles to help practicing programmers up their game and remain relevant in the face of changing trends and an evolving language. The book enhances the traditional design patterns with Java’s new functional programming features, such as functional interfaces and lambda expressions. The result is a fresh treatment of design patterns that expands their power and applicability, and reflects current best practice. The book examines some well-designed classes from the Java class library, using them to illustrate the various object-oriented principles and patterns under discussion. Not only does this approach provide good, practical examples, but you will learn useful library classes you might not otherwise know about. The design of a simplified banking program is introduced in chapter 1 in a non-object-oriented incarnation and the example is carried through all chapters. You can see the object orientation develop as various design principles are progressively applied throughout the book to produce a refined, fully object-oriented version of the program in the final chapter. What You’ll Learn Create well-designed programs, and identify and improve poorly-designed ones Build a professional-level understanding of polymorphism and its use in Java interfaces and class hierarchies Apply classic design patterns to Java programming problems while respecting the modern features of the Java language Take advantage of classes from the Java library to facilitate the implementation of design patterns in your programs
STM32CubeIDE是一款由ST公司开发的集成开发环境,用于开发和调试STM32微控制器的应用程序。它基于Eclipse平台,提供了一套完整的工具链,能够在Windows、Linux和MacOS等操作系统上运行。 STM32CubeIDE的主要特点有以下几点: 1. 统一的开发环境:STM32CubeIDE集成了STM32CubeMX和各种工具,使得开发者能够在一个界面下完成从硬件配置到应用程序开发的整个过程。这样可以极大地提高开发效率,减少开发周期。 2. 包含丰富的软件库和驱动程序:STM32CubeIDE提供了丰富的软件库和驱动程序,可以满足不同应用场景的需求。开发者可以通过图形化的界面进行配置,快速生成驱动代码,并且可以方便地进行修改和扩展。 3. 支持多种编程语言:STM32CubeIDE支持C语言和C++语言的开发,并且可以与其他常见的编程语言进行集成。这使得开发者可以根据自己的需求选择合适的编程语言,实现更灵活的开发。 4. 提供强大的调试功能:STM32CubeIDE具有强大的调试功能,可以通过调试器和仿真器进行实时调试和跟踪。开发者可以监测和修改代码的执行过程,及时发现和修复错误,提高软件的可靠性。 总之,STM32CubeIDE是一款功能强大且易于使用的集成开发环境,适用于初学者和有经验的开发者。它提供了一整套的工具和资源,帮助开发者快速、高效地开发STM32微控制器的应用程序。无论是简单的单片机应用还是复杂的嵌入式系统,STM32CubeIDE都能够满足开发者的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值