管理范围缩小到int的规则 [英] Rules governing narrowing of double to int

查看:105
本文介绍了管理范围缩小到int的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意我不是在寻找代码来强制转换或缩小double到int。

Please note I am NOT looking for code to cast or narrow a double to int.

根据 JLS - $ 5.1.3缩小原始转换


将有符号整数缩小转换为整数类型T
只丢弃除n个最低位之外的所有位,其中n是数字
用于表示类型T的位数。

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.

所以,当我尝试缩小260(二进制表示为 100000100 )到一个字节然后结果为4,因为最低8位是 00000100 这是小数4或长值4294967296L(二进制表示 100000000000000000000000000000000 )到一个字节,然后结果为0.。

So, when I try to narrow a 260 (binary representation as 100000100) to a byte then result is 4 because the lowest 8 bits is 00000100 which is a decimal 4 OR a long value 4294967296L (binary representation 100000000000000000000000000000000) to a byte then result is 0.

现在,为什么我想知道缩小规则的规则double to int,byte等是当我缩小 double 4294967296.0 然后结果是 2147483647 但是当我缩小一个长的 4294967296L 值时,结果是 0

Now, why I want to know the rule for narrowing rule from double to int, byte etc. is when I narrow a double value 4294967296.0 then result is 2147483647 but when I narrow a long 4294967296L value then result is 0.

我已经理解了int,byte等的长时间缩小(丢弃了除n个最低位之外的所有位)但是我想知道底下的内容是什么双重缩小。

I have understood the long narrowing to int, byte etc. (discards all but the n lowest order bits) but I want to know what is going under the hoods in case of double narrowing.

推荐答案


我已经理解了对int,byte等的长缩小(丢弃)除了n个最低位之外的所有位)但我想知道在双重缩小的情况下会发生什么事情。

I have understood the long narrowing to int, byte etc. (discards all but the n lowest order bits) but I want to know what is going under the hoods in case of double narrowing.

...我想了解为什么和如何部分。

... I want to understand the why and how part.




  1. JLS( JLS 5.1.3 )指定结果。简化版本( int )是:

  1. The JLS (JLS 5.1.3) specifies what the result is. A simplified version (for int) is:


  • NaN变为零

  • Inf变为max-int或min-int

  • 否则:


    • 向零舍入以获得数学整数

    • 如果舍入数字对于 int 而言太大,则结果变为min-int或max-int

    • a NaN becomes zero
    • an Inf becomes "max-int" or "min-int"
    • otherwise:
      • round towards zero to get a mathematical integer
      • if the rounded number is too big for an int, the result becomes "min-int" or "max-int"

      具体实施方式如何。有关如何实现的示例,请查看Hotspot源代码(OpenJDK版本)或获取JIT编译器以转储一些本机代码供您查看。 (我想原生代码映射使用单个指令进行实际转换....但我没有检查过。)

      "How" is implementation specific. For examples of how it could be implemented, look at the Hotspot source code (OpenJDK version) or get the JIT compiler to dump some native code for you to look at. (I imagine that the native code maps uses a single instruction to do the actual conversion .... but I haven't checked.)

      为什么是不可知的......除非你能问一位原始的Java设计师/规范作者。一个看似合理的解释是:

      "Why" is unknowable ... unless you can ask one of the original Java designers / spec authors. A plausible explanation is a combination of:


      • 这很容易理解

      • 它与C / C ++,

      • 它可以在通用硬件平台上高效实现,而且

      • 它优于设计师所考虑的(假设的)替代品。

      • it is easy to understand
      • it is consistent with C / C++,
      • it can be implemented efficiently on common hardware platforms, and
      • it is better than (hypothetical) alternatives that the designers considered.

      (例如,抛出NaN,Inf的异常,超出范围会与其他原始转换不一致,并且可能实施成本更高。)

      (For example, throwing an exception for NaN, Inf, out-of-range would be inconsistent with other primitive conversions, and could be more expensive to implement.)

      这篇关于管理范围缩小到int的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆