Java 我如何使用浮点数/双精度数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2947044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:49:00  来源:igfitidea点击:

How do I use modulus for float/double?

javamodulus

提问by ShrimpCrackers

I'm creating an RPN calculator for a school project. I'm having trouble with the modulus operator. Since we're using the double data type, modulus won't work on floating point numbers. For example, 0.5 % 0.3 should return 0.2 but I'm getting a division by zero exception.

我正在为学校项目创建 RPN 计算器。我在使用模数运算符时遇到了问题。由于我们使用的是 double 数据类型,因此模数不适用于浮点数。例如, 0.5 % 0.3 应该返回 0.2 但我得到除以零异常。

The instruction says to use fmod(). I've looked everywhere for fmod(), including javadocs but I can't find it. I'm starting to think it's a method I'm going to have to create?

该指令说使用 fmod()。我到处寻找 fmod(),包括 javadoc,但我找不到它。我开始认为这是我必须创建的一种方法?

edit: hmm, strange. I just plugged in those numbers again and it seems to be working fine...but just in case. Do I need to watch out using the mod operator in Java when using floating types? I know something like this can't be done in C++ (I think).

编辑:嗯,奇怪。我只是再次插入这些数字,它似乎工作正常......但以防万一。使用浮动类型时,我需要注意在 Java 中使用 mod 运算符吗?我知道这样的事情不能在 C++ 中完成(我认为)。

采纳答案by RodeoClown

You probably had a typo when you first ran it.

当你第一次运行它时,你可能有一个错字。

evaluating 0.5 % 0.3returns '0.2' (A double) as expected.

0.5 % 0.3正如预期的那样,评估返回 '​​0.2' (A double)。

Mindprod has a good overview of how modulus worksin Java.

Mindprod很好地概述了模数在 Java 中的工作原理

回答by WhirlWind

I thought the regular modulus operator would work for this in java, but it can't be hard to code. Just divide the numerator by the denominator, and take the integer portion of the result. Multiply that by the denominator, and subtract the result from the numerator.

我认为常规模数运算符可以在 java 中用于此,但编码并不难。只需将分子除以分母,然后取结果的整数部分。乘以分母,然后从分子中减去结果。

x = n / d
xint = Integer portion of x
result = n - d * xint

回答by Michael Mrozek

fmodis the standard C function for handling floating-point modulus; I imagine your source was saying that Java handles floating-point modulus the same as C's fmodfunction. In Java you can use the %operator on doubles the same as on integers:

fmod是用于处理浮点模数的标准 C 函数;我想你的消息来源是说 Java 处理浮点模数与 C 的fmod函数相同。在 Java 中,您可以%像在整数上一样在双精度上使用运算符:

int x = 5 % 3; // x = 2
double y = .5 % .3; // y = .2

回答by Matthew Flaschen

Unlike C, Java allows using the % for both integer and floating point and (unlike C89 and C++) it is well-defined for all inputs (including negatives):

与 C 不同,Java 允许对整数和浮点使用 %,并且(与 C89 和 C++ 不同)它对所有输入(包括负数)都有明确定义:

From JLS §15.17.3:

JLS §15.17.3

The result of a floating-point remainder operation is determined by the rules of IEEE 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.

浮点余数运算的结果由 IEEE 算术规则确定:

  • 如果任一操作数为 NaN,则结果为 NaN。
  • 如果结果不是 NaN,则结果的符号等于被除数的符号。
  • 如果被除数为无穷大,或除数为零,或两者兼有,则结果为 NaN。
  • 如果被除数是有限的,而除数是无穷大,则结果等于被除数。
  • 如果被除数为零且除数有限,则结果等于被除数。
  • 在其余情况下,既不涉及无穷大,也不涉及零,也不涉及 NaN,被除数 n 除以除数 d 的浮点余数 r 由数学关系 r=n-(d·q ) 其中 q 是一个整数,仅当 n/d 为负时为负,仅当 n/d 为正时为正,并且其大小尽可能大,但不超过 n 和 d 的真实数学商的大小。

So for your example, 0.5/0.3 = 1.6... . q has the same sign (positive) as 0.5 (the dividend), and the magnitude is 1 (integer with largest magnitude not exceeding magnitude of 1.6...), and r = 0.5 - (0.3 * 1) = 0.2

因此,对于您的示例, 0.5/0.3 = 1.6... 。q与0.5(被除数)同号(正),大小为1(最大不超过1.6大小的整数...),r = 0.5 - (0.3 * 1) = 0.2