[英]Which rounding method is used in Java when operating on floating point numbers?
The language is Java and the machine is a normal PC with an Intel processor. 语言是Java,机器是带有Intel处理器的普通PC。 The type is double.
类型为double。
Floating point numbers can't represent exactly every fractional number, the result of the operations like division or subtraction are somehow rounded to some near floating point representation. 浮点数不能精确地表示每个小数,除法或减法之类的运算结果以某种方式四舍五入为近似的浮点数表示形式。
I am asking whether the used rounding strategy is: 我问使用的舍入策略是否为:
Java Virtual Machine Floating-Point Arithmetic and IEEE 754
Java虚拟机浮点算法和IEEE 754
...
...
- The rounding operations of the Java Virtual Machine always use IEEE 754 round to nearest mode.
Java虚拟机的舍入操作始终使用IEEE 754舍入到最近模式。 Inexact results are rounded to the nearest representable value, with ties going to the value with a zero least-significant bit .
不精确的结果四舍五入到最接近的可表示值,并以最低有效位为零的值舍入 。 This is the IEEE 754 default mode.
这是IEEE 754的默认模式。 But Java Virtual Machine instructions that convert values of floating-point types to values of integral types round toward zero.
但是,将浮点类型的值转换为整数类型的值的Java虚拟机指令四舍五入为零。 The Java Virtual Machine does not give any means to change the floating-point rounding mode .
Java虚拟机不提供任何更改浮点舍入模式的方法 。
Thus, as Patricia Shanahan already answered in the comments, JVM's rounding mode is fixed to #7 rounding to nearest (half even) from your list. 因此,正如Patricia Shanahan在评论中已经回答的那样,JVM的舍入模式固定为#7 舍入到列表中最接近的值(半数) 。 However this only applies to float-to-float rounding.
但是,这仅适用于浮点间的舍入。 Float-to-integer rounding uses #2 rounding towards zero
浮点数到整数舍入使用#2 舍入为零
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.