java - 在Java中从double转换为float

标签 java floating-point double type-conversion

如果我正在使用 double,并将其转换为 float,这究竟是如何工作的?该值是否被截断以适合 float ?或者值是否以不同的方式四舍五入?对不起,如果这听起来有点补救,但我试图掌握 floatdouble 转换的概念。

最佳答案

来自 Java Language Specification, section 5.1.3 :

A narrowing primitive conversion from double to float is governed by the IEEE 754 rounding rules (§4.2.4). This conversion can lose precision, but also lose range, resulting in a float zero from a nonzero double and a float infinity from a finite double. A double NaN is converted to a float NaN and a double infinity is converted to the same-signed float infinity.

section 4.2.4说:

The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to nearest.

关于java - 在Java中从double转换为float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10075058/

相关文章:

java - 从 javascript 调用时,已签名的小程序给出 AccessControlException : access denied,

c++ - 如何检查 C++ 编译器是否使用 IEEE 754 浮点标准

c++ - C++ 中的异常处理 : Throwing a double when using "throw(int)"

java - GXT : Cancel click event on grid header

JAVA SSL 为服务器端的每个客户端生成一个新的 keystore 证书

java - 从使用外部类的 ArrayList 获取变量

java - Java中的 float 是什么?

c++ - 访问 float 的 4 个字节是否会破坏 C++ 别名规则

Java 故障?减数?

Java - 将字符串转换为 double 的最有效方法