Java_语法基础_取余操作符 %

首先我们来看下 The Java? Language Specification 中官方对它的定义:

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.

In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.

The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.

This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).

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.

If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.

大体的意思就是说:Java中的%代表求余(注意:不是求模),在C和C++中,%仅支持整数操作,而在Java中,不仅接受整数操作,而且接受浮点数,此运算符计算的结果的符号与被除数的符号相同,如果除数为0,将抛出ArithmeticException。

例:

package deep;

public class Test {
    public static void main(String[] args) {
        int a = 5 % 3; // 2
        int b = 5 / 3; // 1
        System.out.println("5%3 produces " + a + " (note that 5/3 produces "
                + b + ")");

        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)

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

再看看官方对浮点数的提及:

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.

例:

package deep;

public class Test {
    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.0

5.0%(-3.0) produces 2.0

(-5.0)%3.0 produces -2.0

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

时间: 2024-07-31 18:05:00

Java_语法基础_取余操作符 %的相关文章

printf()_scanf()_取余运算符与取模运算符

基本的输入和输出函数的用法 printf();四种用法 1.printf("字符串\n"); 2.printf("输出控制符",输出参数); 3.printf("输出控制符1 输出控制符2 ...",输出参数1,输出参数2); 4.printf("输出控制符 非输出控制符",输出参数); 输出控制符有如下: %d %ld %f %lf %x(or %X or %#X or %#x) 最好用%#X 输出如0X2F %o %s s

HDU 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你(取余技巧)

悼念512汶川大地震遇难同胞--一定要记住我爱你 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9582    Accepted Submission(s): 5826 Problem Description 当抢救人员发现她的时候,她已经死了,是被垮塌下来的房子压死的,透过那一堆废墟的的间隙可以看到她死亡的姿势,双膝跪着,整个上身向

salesforce 零基础学习(四十三)运算取余

工作中遇到一个简单的小问题,判断两个数是否整除,如果不整除,获取相关的余数. 习惯java的我毫不犹豫的写下了代码 public Boolean isDivisibility(Integer dividend,Integer divider) { return dividend % divider == 0; } 提交代码发现竟然提交不上?? 后来查看API发现apex中没有直接的%取余运算,所以如果想要取余以及判断是否整除需要其他方式,代码如下: public without sharing

30 May 18 Javascript语法基础

30 May 18 # 图片太多,详细见link 以及文本 一.每日面试(知识复习) 1.问:执行完下面的代码后,  l,m的内容分别是什么? def func(m): for k,v in m.items(): m[k+2] = v+2 m = {1: 2, 3: 4} l = m  # 浅拷贝 l[9] = 10 func(l) m[7] = 8 print("l:", l) print("m:", m) # 报错:dictionary changed size

java语法基础一

Java语法基础一 Java代码基本格式 Java中所有程序代码都必须存在于一个类中,用class关键字定义类,在class之前可以有一些修饰符.格式如下: 修饰符 class 类名 { 程序代码 } 注:1.Java是严格区分大小写的. 2.Java程序中一句连续的字符串不能分开在两行中写. Java程序的注释 Java里的注释有三种类型: 1.单行注释 在注释内容前面加“//”,格式为: 代码; //注释内容 2.多行注释 以斜杠加星号开头,以星号加斜杠结尾. 3.文档注释 以斜杠加两个星号

C#-01.语法基础

a. 语法基础 i. 命名空间(namespace):是 C# 中组织代码的方式,用来声明命名空间 1. 语法:namespace 命名空间名称{ //命名空间的声明 } 2. 作用:可以把紧密相关的一些代码放在同一个命名空间中,大大提高管理和使用的效率 3. 与 Java 的不同处:Java 的是使用 package(包) 的关键字,作用是与 namespace 类似 i. using 关键字:用来引用其他命名空间 1. 语法:using 类名; 2. 与 Java 的不同处:Java 的是使

_01JavaSE基础_关键字、标识符、常量、进制、变量、注释、数据类型、数据类型转换

1.关键字: 被java赋予特定含义的单词 特点: 全部小写 注意事项: goto和const作为保留字存在 2.标识符: 给类.接口.方法.变量等起名的字符序列 组成规则: a.英文大小写字母 b.数字字符(0-9) c.$和_ 注意事项: a.不能以数字开头 b.不能是java中的关键字 c.区分大小写 常见命名规则: a.包: 单级:全部小写 举例:liuyi,com 多级:全部小写,单词与单词之间用"."隔开 举例:cn.itcast,com.baidu b.类名及接口名: 每

php语法基础2

PHP数据类型(它是一种弱类型语言) 整型.浮点型.布尔型.字符串型.数组型.对象.空类型.资源型 标量型:整型.浮点型.布尔型和字符串型!所谓的标量类型,本质上只能存储单项信息的类型. 复合型:数组.对象.复合类型的本质是可以存储多项信息! 特殊类型:空类型和资源型 所有的类型在计算机的中都是以二进制的形式存放的! 进制转换: 小数转换为二进制:乘2取整法 十转八:整数部分除八取余,小数部分乘八取整 八进制转换为二进制,可以一拆三: 二进制转换为八进制可以三并一: 十六进制转换为二进制,可以一

JavaScript语法基础(1)

1.JavaScript是什么? 1)定义: JavaScript「JS」是一种高级的.动态的. 弱类型的.解释型的计算机编程脚本语言. 2)原理: 3)组成: 3大部分: ◆ ECMAScript: JS的语法核心 ◆ DOM: Document Object Model: 文档对象模型: 提供访问和操作网页的方法和接口 ◆ BOM: Browser Object Model: 浏览器对象模型: 提供与浏览器交互的方法和接口 4)总结: JS是一种专门为网页交互而设计的. 简单而又复杂的脚本程